项目删除后ListView不刷新

时间:2016-01-12 18:13:38

标签: android listview android-listview

  ListView listViewUpdate;
  ListAdapter adapter;

  listViewUpdate.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            public boolean onItemLongClick(final AdapterView<?> p, View v, final int po, long id) {

                HashMap<String, String> clickedItem = EditDetails.get(po);
                iD=clickedItem.get(Configs.TAG_ID);
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("Delete");
                builder.setMessage("Are you sure you want to delete?");
                builder.setIcon(android.R.drawable.ic_dialog_alert);
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int ii) {
                        delete(iD);
                        listViewUpdate.setAdapter(adapter);
                        ((BaseAdapter) adapter).notifyDataSetChanged();

                    }
                });

                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
                        {
                            public void onClick(DialogInterface dialog, int ii) {
                                dialog.dismiss();
                            }
                        }

                );
                builder.show();
                return true;
            }
        });

        return edit_details;
    }

项目被删除但listView没有刷新。是否必须拥有customBaseAdapter?从MySQL检索数据,我从here获得教程,因为他只使用List适配器。感谢

1 个答案:

答案 0 :(得分:0)

从数组

中删除当前位置后,您可能需要这样做才能刷新列表View
listDO.clear();  
    listDO.addAll(getyourList()); 

     if (rowAdapter == null) {   
         rowAdapter = new RowAdapter(listDO);   
         listView.setAdapter(rowAdapter);   
        } else {   
         rowAdapter.notifyDataSetChanged();   
        }