简体   繁体   English

notifyDataSetChanged()方法未刷新listview

[英]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? 我正在使用notifyDataSetChanged()方法。如何解决此问题?

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. 您可能要在DeleteList的onPostExecute()中的相应适配器上调用notifyDataSetChanged(),因为您是通过Aysnc任务从列表中删除元素的。 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();

            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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