2

I have a list of items. I have a button that sends you to another activity in order to create a new item. When the new item is saved an intent sends you back to the activity with the list where the new item should be added. The adapter extends from BaseAdapter without filtering, it just sets the data to the views.

My code is this:

onCreate(){
    ListView listView = findItemById......
    listView.setListeners.......
}

onResume(){
    data = getData();
    adapter = new ListAdapter(context,data);
    listView.setAdapter(adapter);
}

It works but is this correct?

Can I use creation of adapter and setAdapter in onCreate and use notifyDataSetChanged() only in onResume somehow?

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
mike_x_
  • 1,900
  • 3
  • 35
  • 69
  • 2
    sure, you can create your adapter in `onCreate`, call `setAdapter` there and call `notifyDataSetChanged` inside `onResume` – pskink Oct 05 '17 at 07:54
  • 1
    It depends on how `ListAdapter` is implemented ... fx: if it extends ArrayAdapter and it's using filtering (the only reason to extending from ArrayAdapter not from BaseAdapter) then well it may not work but if you are extending BaseAdapter and using passed `data` references directly then yes ... rember that if you do `data = getData();` it will not work as after this `data` would be a different reference then passed to Adapter previously – Selvin Oct 05 '17 at 07:55
  • 1
    You might find this helpful :- https://stackoverflow.com/questions/14503006/android-listview-not-refreshing-after-notifydatasetchanged – ANUJ GUPTA Oct 05 '17 at 07:56
  • Is the above a bad practice? – mike_x_ Oct 05 '17 at 08:12
  • 1
    sort of `a bad practice` but if it works then dont worry... – pskink Oct 05 '17 at 08:16
  • adapter.notifyDataSetChanged(); – Pravin Fofariya Oct 05 '17 at 08:21

3 Answers3

2

I think the correct way to do it is a use

startActivityForResult(...)

method to launch your Second activity (where your new items is added).

And in your second activity use

setResults(RESULT_OK or RESULT_CANCELLED)

(diffrent result when new items is added or user just back to prev screen).

Then in your ListAdapter class add method

public void updateData(List data) {
    this.data = new ArrayList<>(data);
    notifyDataSetChanged();
}

And call it in your onActivityResults(...)

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == YOUR_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        adapter.updateData(...your data from result Intent, or from anywhere else...);
    }
}

Also i strongly recommend you to use RecyclerView instead of ListView

UPD: to answer your question - i think that biggest mistake in your code, is to create new instance of ListAdapter object in onResume() method, when it's not necessary

Hank Moody
  • 354
  • 2
  • 15
1

Add this method to your adapter and then call it with new data using adapter.udpateData(data):

public void updateData(List data) {

  this.data = data;
  notifyDataSetChanged();
}

Also, you can use notifyItemRangeInserted(0, data.size())

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
0

Hi OnActivityResult read all data create the new object and add to the list and call notifyDatasetChanged() , i will automatically refresh ui

Shanmugam
  • 301
  • 1
  • 10