So, I have MainActivity with ArrayList< MyObject>, ListView for display it and EditActivity to get UI for editing items. Adapter for ListView extends ArrayAdapter< MyObject>. When user click on item I want to start EditActivity with object for editing. How could I put the object to EditActivity? I have:
Intent i = new Intent(this, EditActivity.class);
startActivity(i);
how could I get the object in EditActivity?
Of course, I could declare ArrayList< MyObject> as static and put index of the item with:
Intent i = new Intent(this, EditActivity.class);
i.putExtra("index", iItemIdex);
startActivity(i);
and then, in EditActivity, get it like:
int iIndex = getIntent().getExtras().getInt("index");
MyObject o = MainActivity.MyArray.get(iIndex);
but I guess that is not best decision 🙂
You don’t have to declare your List static. Here is the code you should have to get it to work (one possibility) :
This way there is no useless static variables, and you are using the method the way it has been made for. One of the easiest and cleanest solution IMO.
Be carefull, to use this method, your have to redefine the
getItem(int)method in your customArrayAdapter. You should do this :EDIT : Then if you want to be able to remove items, I think you should put the whole list containing your objects in the intent (don’t declare it static). Then just call add()/remove() methods, and when you want to update UI to show the modifed list, just call notifyDataSetChanged() on your custom ArrayAdapter, for example when returning in MainActivity :