Android : Best option for recognize clicked item in listview?

on Saturday, August 9, 2014


In My app I have a listview and a button for creating albums (By clicking on button app a new item adds to listview) and by clicking on each listview items you can go to that album and upload/take your photos for display.


for saving and retrieving photos after take/pick I use SharedPrefrence for saving and retrieving Image Path. as you see I Can't have a predefined SharedPrefrence, so I use this method which I made it myself: in listview OnItemClickListener I make this "str" String and by putting it in a intent It will be passed to my first album page:



list.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {

String str = new String("album" + arg2);
Intent intent = new Intent(Albums.this, MainA.class);
intent.putExtra("extra", str);
startActivity(intent);
}
});


And after this process I use this string in my new activity for my sharedprefrence like this:



String str = getIntent().getExtras().getString("extra");
SharedPreferences settings = getSharedPreferences(str, 0);


in this way everything does perfect but except when for example User Deletes the second album (second Item in Lisview) which cause third album to be the second and since I used the position int for specify the sharedprefrence it will totally destroy.


I don't know if you could understand my way =), But if you did what should I do to avoid this?


this way even is the best way for my purpose?


thanks.




EDIT:


deleting listview item code:



public void deletetask(String t){
if (null == itemArrey) {
itemArrey = new ArrayList<String>();
}
itemArrey.remove(t);

//save the task list to preference
b1s = getSharedPreferences("pref", Context.MODE_PRIVATE);
try {
b1s.edit().putString("array", ObjectSerializer.serialize(itemArrey)).commit();
} catch (IOException e) {
e.printStackTrace();
}
}

0 comments:

Post a Comment