The array adapter notifyDataSetChanged () will not work

I spent too much time on this, so I need to ask again. I have no idea why this is happening at all.

I have an array adapter (aAdapter) and a list of arrays (aList), and I'm trying to put a clear button to erase entries in the database and clear the list.

My problem is that NotifyDataSetChanged () just doesn't work from my onlick method:

public void clearDB(View view) { aList.clear(); aAdapter.notifyDataSetChanged(); HighScoresDB hsdb = new HighScoresDB(HighScoresActivity.this); hsdb.openDB(); hsdb.clearDB(); hsdb.closeDB(); } 

It works everywhere though. I even tried putting clear and notifyDataSetChanged () in another method and calling it, but that also did not work, but I was working when I called it from onCreate ....

Any ideas?

ps the database is cleared.

+6
source share
5 answers

Firstly, I found that the implementation of the Android adapter is very wrong. When it comes to doing something to order, there seems to be ambiguous data on how to use it, and official documentation does not specify any of them. I would be very happy to be in this wrong.

The way I got consistent results when editing the data in the view was as follows:

  • All changes to the presented data structure should be made in AsyncTask , which makes sense when you change things about the user interface and do not want to have problems with concurrency.

  • Operations on basic data structures should be performed by calling adapter methods, so if you have a ListAdapter , then you use the add , remove and clear the list adapter. This means that the adapter controls viewing notifications, etc. This usually leads to the need to create a custom adapter, since the available methods are limited (even before adding all in sdk versions to 7). You also get your adapter acting like a big fat controller , although I know that we should not consider android as an MVC pattern, it still seems wrong.

  • I created applications in which I bypassed the adapter’s calls to work with the basic data structure, and it worked all through the results, turned out to be unpredictable if you did not manage viewing notifications. Now I'm just calling through the adapter.

So, although I can’t explain why in notifiyDataSetChanged doesn’t work specifically in your onClick method. I hope to get some useful information that can help you get your application to work as expected.

+12
source

As long as you are not very pretty, you can simply reinitialize the adapter instead of notifying, I saw that sometimes this is the only way to make it work.

+9
source

Thus, I dealt with a similar problem, it is basically reinitializing the adapter, for example, as shown in the blindstuff file.

  public class Example extends Activity{ CustomAdapter adapter; ArrayList<ArrayList<String>> info = new ArrayList<ArrayList<String>>(); final ListView list = (ListView) findViewById(R.id.listView_custom); adapter = new CustomAdapter(this, diceInfo.get(id)); list.setAdapter(adapter); 

Then in the onclick Listener

 add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { info.get(id).add("1,0,0,true"); adapter = new CustomAdapter(Example.this, info.get(id)); list.setAdapter(adapter); } }); 

The example does not have everything initialized, but it comes to the point. I just create a new adapter and install it in the list that I have. It works well.

+2
source

Also faced with strange behavior of the adapter when it is used in a dialog box. After updating the adapter (or the basic structure - in both directions) and the notifiyDataSetChanged dialog without errors or other tracing in LogCat or debugging Eclipse. Exactly the same code, once the adapter is applied to normal activity (ListView), it works just fine.

0
source

I think this thread is no longer inactive, but for future reference:

According to developer.android.com ,

[onNotifyDataSetChanged ()] Tells attached observers that the underlying data has been changed, and any view that reflects the data set should be updated.

Instead of adapting the View adapter for updating, simply update the data with adapter.clear () and adapter.add (), and then force the ListView to be updated again by calling listView.setAdapter (adapter).

0
source

Source: https://habr.com/ru/post/905644/


All Articles