Refresh listview onpost execute asynctask

2.9k views Asked by At

i know this question has been posted multiple times and i browsed almost all of them but there is not result, i am performing a deleting an item from mysql database but it is not refreshing, here is the code of the onclicklistener and the button:

onClick listener:

 holder.void_button.setOnClickListener(new OnClickListener(){

    public void onClick(View v) {
    adapter = new CustomListViewVoidAdapter(context,R.layout.mytemp, items);
    item_selected= items.get(position);
        new DeleteOrder().execute();
               }});
            vi.setTag(holder);
        }

OnPostExecute from AsyncTask:

 protected void onPostExecute(String unused){
            adapter.remove(item_selected);
            adapter.notifyDataSetChanged();
        }

the adapter is instatiated globally, can you please check where the problem might be? it is not returning any error, just deleting the item and not refreshing.

Regards

Ralph

3

There are 3 answers

0
sha256 On

You better set the adapter again to the list view in onPostExecute with the new values. And you don't need to call notifyDataSetChangedin this case. Also don't re-intialize the adapter in onClick, this is not neccessary.

3
Paul On

Throwing it out there, but, have you tried adapter.notifyDataSetInvalidated();? That forces an update.

Also, put the code in the asynctask!

Like such:

protected void onPostExecute() {

   adapter.notifyDataSetChanged();
   adapter.notifyDataSetInvalidated();


}
4
sanky jain On

Add below line in postExecute.

if(adapter != null) { 
    adapter = new CustomListViewVoidAdapter(context,R.layout.mytemp, items);
    YourListviewObject.setAdapter(adapter); 
}