简体   繁体   中英

notifyDataSetChanged() method is not refreshhing listview

I am performing delete and delete all operations in my app on button click. when I click on delete button item is deleted from list view as well deleted from the server.When I click on delete all list view is not refreshing or updating at the same time.In delete all case server is updated but listview is not updated. I am using notifyDataSetChanged() method.How I can resolve this problem?

public void alertMessage()
            {
                DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        switch (which){
                            //for cancel button
                            case DialogInterface.BUTTON_POSITIVE:
                                Category_Dashboard_Page activity = (Category_Dashboard_Page) context;
                                activity.swipeCategorylistView.closeAnimate(position);

                                break;
                            //for delete
                            case DialogInterface.BUTTON_NEGATIVE:
                                Category_Dashboard_Page activity1 = (Category_Dashboard_Page) context;
                                activity1.swipeCategorylistView.closeAnimate(position);
                                activity1.categoryList_items_obj.remove(position);
                                activity1.categoryAdapter.notifyDataSetChanged();
                                new DeleteList().execute();
                                break;
                            //for delete all
                            case DialogInterface.BUTTON_NEUTRAL:
                                Category_Dashboard_Page activity2 = (Category_Dashboard_Page) context;
                                activity2.swipeCategorylistView.closeAnimate(position);

                                  for(int i=0;i<categoryList_items_obj.size();i++)
                                   {
                                      Category_Dashboard_Page.CategoryList_Item category_list_item = categoryList_items_obj.get(i);
                                      System.out.println(category_list_item.getCategory_id());

                                      if(category_list_item.getCategory_id().equalsIgnoreCase("all"))
                                      {
                                          categoryList_items_obj.remove(i);
                                      }
                                   }

                               // notifyDataSetChanged();
                                System.out.println(String.valueOf(getCount()));
                                System.out.println(String.valueOf(categoryList_items_obj.size()));
                                activity2.categoryAdapter.notifyDataSetChanged();
                                new DeleteAllList().execute();
                                break;

                        }

                    }
                };
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("Are you sure?");
                builder.setMessage("You want to delete this article from all categories.")
                        .setPositiveButton("Cancel ", dialogClickListener)
                        .setNegativeButton("Delete ", dialogClickListener)
                        .setNeutralButton("Delete all", dialogClickListener).show();

            }

You may want to call notifyDataSetChanged() on the corresponding adapter in onPostExecute() of DeleteList, since you are deleting the elements from the list through Aysnc task. Let me know if this solved your problem. If not, please share some more code of your project.

You need to remove the objects from the adapter also. Try the following code -

public void alertMessage()
            {
                DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        switch (which){
                            //for cancel button
                            case DialogInterface.BUTTON_POSITIVE:
                                Category_Dashboard_Page activity = (Category_Dashboard_Page) context;
                                activity.swipeCategorylistView.closeAnimate(position);

                                break;
                            //for delete
                            case DialogInterface.BUTTON_NEGATIVE:
                                Category_Dashboard_Page activity1 = (Category_Dashboard_Page) context;
                                activity1.swipeCategorylistView.closeAnimate(position);
                                activity1.categoryList_items_obj.remove(position);
                                activity1.categoryAdapter.remove(position);
                                activity1.categoryAdapter.notifyDataSetChanged();
                                new DeleteList().execute();
                                break;
                            //for delete all
                            case DialogInterface.BUTTON_NEUTRAL:
                                Category_Dashboard_Page activity2 = (Category_Dashboard_Page) context;
                                activity2.swipeCategorylistView.closeAnimate(position);

                                  for(int i=0;i<categoryList_items_obj.size();i++)
                                   {
                                      Category_Dashboard_Page.CategoryList_Item category_list_item = categoryList_items_obj.get(i);
                                      System.out.println(category_list_item.getCategory_id());

                                      if(category_list_item.getCategory_id().equalsIgnoreCase("all"))
                                      {
                                          categoryList_items_obj.remove(i);
                                          categoryAdapter.remove(i);
                                      }
                                   }

                               // notifyDataSetChanged();
                                System.out.println(String.valueOf(getCount()));
                                System.out.println(String.valueOf(categoryList_items_obj.size()));
                                activity2.categoryAdapter.notifyDataSetChanged();
                                new DeleteAllList().execute();
                                break;

                        }

                    }
                };
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("Are you sure?");
                builder.setMessage("You want to delete this article from all categories.")
                        .setPositiveButton("Cancel ", dialogClickListener)
                        .setNegativeButton("Delete ", dialogClickListener)
                        .setNeutralButton("Delete all", dialogClickListener).show();

            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM