refresh listview created from an arraylist after delete operation

耗尽温柔 提交于 2019-12-25 07:42:54

问题


I have seen too many questions about refreshin a listview and I have tried every solution on the topics but none worked for me.

have tried notifysetdatachange(), invalidateviews, refreshdrawablestate() but none worked.

I have an arraylist and after an longclick operation I am deleting an item from arraylist.and I want it to disappear from listview.but it does not.

but after deletion if I open another activity and turn back to the activity which contains my listview it disappears.

so my deletion operation is successful, I just want to know how to refresh listview.,

I did not post a code because there is nothing unique I am just using

I have an arraylist named al_6.I am filling it with objects.

then I send them to the my adapter like

adapter = new ContentSearchListAdapter(getActivity(), channels2,
                options);
        listview.setAdapter(adapter);

and when I want to delete an object from my arraylist.

al_6.remove(position)

and as I said before the deletion operation is successful


回答1:


Your should use these syntax for achieving it

((YourAdaptor)Listview.getAdaptor()).notifyDataSetChange();

I hope its helpful to you...:)




回答2:


Try this code and for your adapter class,

   // query fire on your deletion operation
   mArrayList.remove(position);
   notifyDataSetChanged();



回答3:


From my experience, it is strange the behaviour of the listview to refresh it. I always do the same to refresh listView without problems:

  • Use custom Adapter which extends from BaseAdapter
  • Use ArrayList<> structure data into the adapter. If you have your data in a database SQLiteDatabase, I recomend to put the data you need int that list in an ArrayList and pass it to the adapter. I can't do the same with CursorAdapter :(
  • Make your ArrayList and your Adapter public static in your Activity if you plan to modify the data outside the Activity and want to update the list.
  • When you modify or remove any info from your data (if you do it directly on your database, duplicate the same into the ArrayList), use the public static reference of the Adapter of your Activity to call his method notifyDataSetChanged()


来源:https://stackoverflow.com/questions/14099299/refresh-listview-created-from-an-arraylist-after-delete-operation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!