I have a question regarding the notifyDataSetChanged() method of Adapter.

Asked 2 years ago, Updated 2 years ago, 133 views

Hello. I am studying Android while making a memo application.

The Notepad contains ListView and the Adapter contains CustomAdapter, which is defined by inheriting the BaseAdapter. There is only one text view in the list view.

If you press the [Add note] button, a new activity will be displayed. If you type a note in it and press "Finished", Insert the memo you received from the onActivityResult into the ArrayList and call adapter.notifyDataSetChanged().

ArrayList<MemoItem> mArrayList = new ArrayList<MemoItem>();
Context mContext;

public MemoAdapter(ArrayList<MemoItem> arrayList, Context context) {
        this.mArrayList = arrayList;
        this.mContext = context;
}

Place an ArrayList as a field inside the CustomAdapter class

mMemoAdapter = new MemoAdapter(mArrayList, getApplicationContext());

When you create an adapter in an activity, you are supposed to hand over the mArrayList that you have in the adapter.

So, the onActivityResult of the activity is called, and if you add one item by doing mArrayList.add() in the activity, it points to the same thing as ArrayList in the adapter, so I thought notifyDataSetChanged() would update the list view.

There was a new item in the list view, but the problem was that the contents of it were not visible, or that it printed out something like the one that already exists.

When I googled the relevant information as adapter notifyDataSetChanged, there are a lot of things, but I don't understand well, so I'm asking you this question.

Some of the answers in the stack overflow were to create a new adapter in the activity and do listview.setAdapter again, but I didn't use that method because I thought it wouldn't be good for performance.

Is there any way to solve this problem? Or why this is... Is there a problem with the notifyDataSetChanged method...

android adapter listview

2022-09-22 12:23

1 Answers

When calling notifyDataSetChanged, is it being called in the Ui thread?

 // How to call notifyDataSetChanged() using Thread in charge of UI
  getContext().runOnUiThread(new Runnable() {
      public void run() {
          adapter.notifyDataSetChanged();
      }   
 });

And if you create a new adapter and call back the setAdapter... The scroll position is initialized. Regardless of the performance issue, the result will not be easy for users to use.

[Additional Answer]

Then try updating the entire list you used to create a Custom Adapter.

temporalList.addAll(adapterList); // Include all the contents of the list connected to the adapter in the temporary list
temporaryList.add(newItem); // After registering a new item,
Clear the contents of the list connected to the adapterList.clear(); // adapter
adapterList.addAll(temporalList); // Add all the contents of the temporary list to the list connected to the adapter
Call adapter.notifyDataSetChanged(); // and notifyDataSetChanged.

It's not neat after all. Other options are CursorAdapter (link1, link2, link3).

Other reference: Link 4


2022-09-22 12:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.