Best Way to Update Adapter / ListView on Android

My book "Hello Android" gives this as a way to use the db user helper, adjust the cursor, and then configure the adapter as follows:

Cursor cursor CustomDatabaseHelper test = new CustomDatabaseHelper(this); try { cursor = getData(); showData(cursor); } finally { test.close(); } 

With this, however, every time I need to update a dataset, I need to continue to work with this block of code (which is a bit difficult inside onClick () for the button due to the lack of "this".

Is this the best way to update the dataset, or should I look at removing .close and issuing the adapter .notifyDataSetChanged ()? If I do this, sometimes I get power like (and I can’t remember at the moment), but sometimes it can’t delete correctly. I think it could be because the database is currently open and it is trying to open again.

Should we also declare variables for cursors, DatabaseHelpers and Adapter in the class (outside OnCreate) so that they are accessible for all functions?

I understand that this is just bad programming at this point, but I'm trying to get some guidance on the best way to do something.

+47
android refresh listview
Nov 16 2018-10-16
source share
9 answers

You should use adapter.notifyDataSetChanged() . What do logs say when used?

+78
Nov 16 '10 at 12:54
source share
β€” -

Just add this code before installing the Adapter , it works for me:

  listView.destroyDrawingCache(); listView.setVisibility(ListView.INVISIBLE); listView.setVisibility(ListView.VISIBLE); 

Or Directly, you can use the method below after changing the data resource.

  adapter.notifyDataSetChanged() 
+15
Sep 14 '11 at 13:23
source share

The following code works perfect for me

 EfficientAdapter adp = (EfficientAdapter) QuickList.getAdapter(); adp.UpdateDataList(EfficientAdapter.MY_DATA); adp.notifyDataSetChanged(); QuickList.invalidateViews(); QuickList.scrollBy(0, 0); 
+9
Dec 03 '10 at 18:00
source share

Best Way to Update Adapter / ListView on Android

Not only calling notifyDataSetChanged () will update the ListView data, you must call setAdapter() before loading the information correctly:

  listView.setAdapter(adapter); adapter.notifyDataSetChanged(); 
+3
Mar 31 '17 at 19:52
source share

If you are using LoaderManager, try with this statement:

 getLoaderManager().restartLoader(0, null, this); 
0
Mar 02 '16 at 12:45
source share
 adapter.notifyDataSetChanged(); 
0
Aug 29 '16 at 10:10
source share

Perhaps their problem is when the search is done in the database. In his fragment redefinition loops, his Fragment.java just needs to figure out: try testing using methods:

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_x, container, false); //Your query and ListView code probably will be here Log.i("FragmentX", "Step OnCreateView");// Try with it return rootView; } 

Try putting Log.i ... "onStart" and "onResume" in the same way.

Finally, edit the code in "onCreate" and put it in "onStart", for example:

 @Override public void onStart(){ super.onStart(); Log.i("FragmentX","Step OnStart"); dbManager = new DBManager(getContext()); Cursor cursor = dbManager.getAllNames(); listView = (ListView)getView().findViewById(R.id.lvNames); adapter = new CustomCursorAdapter(getContext(),cursor,0);// your adapter adapter.notifyDataSetChanged(); listView.setAdapter(adapter); } 
0
Sep 25 '16 at 21:22
source share

Just write this code in your Custom ArrayAdaper :

 private List<Cart_items> customListviewList ; refreshEvents(carts); public void refreshEvents(List<Cart_items> events) { this.customListviewList.clear(); this.customListviewList.addAll(events); notifyDataSetChanged(); } 
0
Mar 23 '17 at 8:39 on
source share

just write this code in your Custom ArrayAdaper:

 public void swapItems(ArrayList<Item> arrayList) { this.clear(); this.addAll(arrayList); } 
-one
Aug 29 '16 at 10:08 on
source share



All Articles