1

I'm wondering right now how to implement following functionality in best and most reusable way.

Whe then user clicks an item in the ListView, he goes to DetailsActivity, but when he comes back to ListActivity item that he clicked before is shown as first in the list.

I'm wondering should I implement some kind of sorting in listview or in adapter. Which way would be most reusable and generic eg. clicked item is excluded?

midhunhk
  • 5,560
  • 7
  • 52
  • 83
Axxxon
  • 703
  • 2
  • 11
  • 27

3 Answers3

1

You can use the insert method from ArrayAdapter. When coming back from DetailsActivity, delete the clicked item from your adapter using the remove method, then insert the element at the first position. Of course, you also need to call notifyDataSetChanged when you're done.

To identify when you're coming back from DetailsActivity, you could launch it with startActivityForResult and modify the adapter in onActivityResult. More on that here.

Community
  • 1
  • 1
2Dee
  • 8,609
  • 7
  • 42
  • 53
1

The most simplest thing I could suggest for it is

Remove the item from the list and the add the item to the list on calling on activity result.

Set the data to the adapter (may be using a public method in adapter class)

Call notify data set changed.

Rolcyy
  • 68
  • 5
1

Get the item position clicked from onItemClicklistener and delete the item at that position in arraylist or array and add it to 0th position. So while calling DetailsActivity instead of writing startActivity(Intenet) use startActivityForResult(Intent) and in OnActivityResult method do the above said operation and call notifydatasetchanged().

VINIL SATRASALA
  • 614
  • 5
  • 11