[英]notifyDataSetChanged() method is not refreshhing listview
單擊按鈕后,我正在執行刪除和刪除應用程序中的所有操作。 當我單擊刪除按鈕時,項目將從列表視圖中刪除以及從服務器中刪除。當我單擊刪除時,所有列表視圖不會同時刷新或更新。在刪除所有情況下,服務器已更新但列表視圖未更新。 我正在使用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();
}
您可能要在DeleteList的onPostExecute()中的相應適配器上調用notifyDataSetChanged(),因為您是通過Aysnc任務從列表中刪除元素的。 讓我知道這是否解決了您的問題。 如果沒有,請分享更多項目代碼。
您還需要從適配器中刪除對象。 嘗試以下代碼-
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.