0

I have a listView inside Activity inside tab layout. If I long press the item, I get the option to rename the item. Once the item is renamed, the change can't be seen until the activity is restarted.

I tried solving the problem by simply creating a new intent and re-opening activity, but since that activity is inside tablayout it doesn't work. I also tried re-opening tabLayout activity but then it automatically goes to tab 1, while I'm trying to refresh listView inside tab 2.

So then I tried solving it by creating updateListView() method:

public void updateListView(){
    listAdapter.clear();
    listAdapter.addAll(recordedFilesArray);
    listAdapter.notifyDataSetChanged();
}

But that doesn't work either. When I'm using this method, it completely clears listView and then I have to restart the activity again to see the results.

So, anyone have any idea what I could do to see listView changes without restarting the activity? By the way, if it helps, I'm reading ListView (ArrayList) from a text file.

Guy
  • 6,414
  • 19
  • 66
  • 136
  • See this: http://stackoverflow.com/questions/5991968/how-to-force-an-entire-layout-view-refresh – roky Aug 19 '13 at 14:08

4 Answers4

0

Try using notifyDataSetChanged() only without clearing your listAdapter

Royi
  • 745
  • 8
  • 22
0

You can make a separate adapter class and implement its getView() function to manage this. Your activity will pass the array list to the adapter. The adapter's getView() will take care of displaying the contents from the list.

When the list gets modified, you need to reinitialize the adapter. So that way getView() always have the updated data to display on the screen.

sjdutta
  • 376
  • 1
  • 5
  • Thanks a lot for your answer, but I am new to Android programming so this explanation is very confusing to me. Is there any chance you could give me a small code example with comments? – Guy Aug 19 '13 at 21:14
0

For renaming the item, you can change your item object in the ArrayList, and then call adapter.notifyDataSetChanged().

Saeed
  • 152
  • 3
0

I found the problem. The ArrayList wasn't changed until the activity was restarted. I had to call arrayList.set() to change the element in the ArrayList. Now it works as it should.

My code:

recordedFilesArray.set(toDelete, input.getText().toString()); //toDelete is arg2 variable (onLongItemClickListener)
listAdapter.notifyDataSetChanged();
Guy
  • 6,414
  • 19
  • 66
  • 136