删除ListView数据并更新数据,调用notifyDataSetChanged,但没有更新我的列表

时间:2012-02-09 07:24:54

标签: android android-listview

我有一个带有自定义适配器的ListView,它是从本地sqlite数据库填充的。当用户长按项目并选择“删除”时,会弹出对话框再次确认操作,确认点击后项目将被删除。 我的问题是该项目已从数据库中删除,并且arraylist(适配器的数据集)已更新,但是当我已经调用notifyDataSetChanged()时,ListView不会更新。
//在AlertDialog / PositiveButton点击监听器<上设置删除方法ListView ContextMenu侦听器。

我已经检查或尝试了以下内容,但仍然......
1。重建它(go onCreate),listview会正确显示,所以应该不是其他问题。
2。我调用了相同的适配器,由相同的hashCode确认
3。 mListView.setAdapter((ListViewAdapter)(ListViewer.mListView.getAdapter()));
4。 allResArray.clear(); loadData();

有人能帮帮我吗?谢谢。我已经坚持了2天!!

这是我的代码:

private static ListView mListView;                  
private ListViewAdapter mListViewAdapter;
public String[] strListRID;
public List<List<String>> allResArray;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    
    setView(R.layout.listviewer);
    init(); 
}

public void init(){
    setTitle(getString(R.string.app_name));
    setTitleBar(R.drawable.title_home, 0, R.drawable.title_add, 0, 
            0, 0, R.drawable.titlebar_title, 0);
    initListView();     
}

//MainListView initialization    
private void initListView(){
    boolean SD_STATE = checkSDState();
    allResArray = new ArrayList<List<String>>();
    loadData();

    mListView = (ListView) findViewById(R.id.ListView);
    mListViewAdapter = new ListViewAdapter(this, SD_STATE, allResArray);
    mListView.setAdapter(mListViewAdapter);
    Log.i("mListViewAdapter", String.valueOf(mListViewAdapter.hashCode()));

    mListView.setCacheColorHint(0x00000000);    

    // Click listener to ListView
    mListView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            goIntent(5, strListRID[position]);
        }   
    });   

    // ContextMenu listener to ListView
    mListView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {       
        @Override
        public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
            menu.setHeaderTitle(R.string.conext_menu_title);       
            menu.add(0, 0, 0, R.string.conext_menu_edit);
            menu.add(0, 1, 0, R.string.conext_menu_delete);
        }       
    });  
}

@Override  
public boolean onContextItemSelected(MenuItem item){
    AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {
    case 0:         //Edit
        goIntent(5, strListRID[menuInfo.position]);
        return true;    
    case 1:         //Delete
        showDeleteDialog(strListRID[menuInfo.position], menuInfo.position);
        break;                  
    default:
        break;        
    }
    return super.onContextItemSelected(item); 
}

private void loadData() {
    allResArray = getAllDatabaseRes();
    int size = allResArray.get(0).size();
    strListRID = allResArray.get(0).toArray(new String[size]);
}

/**
 * AlertDialog: confirm before delete data
 * Remove it from ArrayList and notifyDataSetChanged() to adapter
 * @param strListRID
 */
private void showDeleteDialog(final String strListRID, final int position) {
    AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    dialog.setIcon(R.drawable.dialog_notice);
    dialog.setMessage(R.string.text_delete);        
    dialog.setPositiveButton(R.string.btn_delete, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            if (delRes(Integer.parseInt(strListRID))){
                                    int size =((ListViewAdapter)(mListView.getAdapter())).mResArray.size();
                for (int i = 0; i < size; i++){
                    ((ListViewAdapter)(mListView.getAdapter())).mResArray.get(i).remove(position);
                }
                ((ListViewAdapter)(ListViewer.mListView.getAdapter())).notifyDataSetChanged();
                Log.i("mListViewAdapter_check", String.valueOf(((ListViewAdapter)(ListViewer.mListView.getAdapter())).hashCode()));
                showShortToast((String) getResources().getText(R.string.text_record_delete_ok));
            }
            else{
                showShortToast((String) getResources().getText(R.string.text_record_delete_ng));
            }   
        }
    });
    dialog.setNegativeButton(R.string.text_no, null);   
    dialog.show();      
}
BaseActivity中的delRes()代码:

 /**
 * Delete the specific rid record from database
 * @return boolean DEL_OK
 * @throws SQLException
 */
protected boolean delRes(int strRID) throws SQLException {    
    boolean DEL_OK = false;
    String deleteWhere = "rid=?";
    String[] deleteWhereValue = { String.valueOf(strRID)};
    Log.i("rid to delete", deleteWhereValue[0]);

    int delrow = dbHelper.delete(tables[0], deleteWhere, deleteWhereValue);
    Log.i("delrow", String.valueOf(delrow));

    if (delrow == 1){
        DEL_OK = true;
    }
    return DEL_OK;
}

和logCat:

02-09 15:19:59.744: I/mListViewAdapter(28235): 1163110296
02-09 15:19:59.784: I/tmp(28235): /sdcard/Photo/20120208164204.jpg
02-09 15:19:59.814: I/tmp(28235): /sdcard/Photo/20120208163627.jpg
02-09 15:19:59.854: I/tmp(28235): /sdcard/Photo/20120209151917.jpg
02-09 15:20:05.514: I/menuInfo.position(28235): 1
02-09 15:20:08.634: I/rid to delete(28235): 10
02-09 15:20:08.674: I/delrow(28235): 1
02-09 15:20:08.674: I/mListViewAdapter_check(28235): 1163110296

当前解决方案:设置新的listviewadapter

    /**
 * AlertDialog: confirm before delete data
 * Remove it from ArrayList and notifyDataSetChanged() to adapter
 * @param strListRID
 */
private void showDeleteDialog(final String mRID, final int position) {
    AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    dialog.setIcon(R.drawable.dialog_notice);
    dialog.setMessage(R.string.text_delete);        
    dialog.setPositiveButton(R.string.btn_delete, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

            if (delRes(Integer.parseInt(mRID))){    
                int size = allResArray.size();
                for (int i = 0; i < size; i++){
                    allResArray.get(i).remove(position);
                }
                strListRID = allResArray.get(0).toArray(new String[size-1]);

                //((ListViewAdapter)(mListView.getAdapter())).notifyDataSetChanged();
                mListView.setAdapter(new ListViewAdapter(getApplicationContext(), true, allResArray));
                showShortToast((String) getResources().getText(R.string.text_record_delete_ok));
            }
            else{
                showShortToast((String) getResources().getText(R.string.text_record_delete_ng));
            }   
        }
    });
    dialog.setNegativeButton(R.string.text_no, null);   
    dialog.show();      
}

3 个答案:

答案 0 :(得分:1)

尝试删除直接从适配器指定的行,然后调用notifyDataSetChanged()。或者您可以创建新适配器,迭代ArrayList并将数据添加到新适配器,然后将此适配器设置为ListView。

答案 1 :(得分:1)

我见过同样的问题。删除项目在后备存储,并通过调用notifyDataSetChanged()通知适配器后,似乎ListView.confirmCheckedPositionsById()在内部的适配器去了解它的工作响应notifyDataSetChanged(叫)。 ONE解决方案正如您所说的那样,用新的替换适配器 - 但正如您所说,这会弄乱用户在列表中的位置,并且用户必须再次向下滚动 - 这很糟糕。

所以我做的是检查我的派生适配器是否在数组中保存数据时超出范围,当超出范围时,只返回项目ID为0.(注意我在此适配器中有稳定的ID)在这种情况下,我的后备存储是mTC,我在调用适配器调用项ID时执行此操作:

  

类TaskAdapter扩展BaseAdapter实现OnClickListener {

     

...         @覆盖         public long getItemId(int position){             //将来从Task.java中提取tode后更新这个             if(position&gt; = mTC.size())                 返回0;             return mTC.getTask(position).hashCode();         }   ...   }

这解决了问题,BaseAdapter的内部代码似乎意识到最后一项不再有效......一切正常。我希望这有助于那些遇到这个问题的人。

答案 2 :(得分:0)

看来你是ArrayList的所有数据。在这种情况下删除数据库中的条目您可以执行的操作如下,

  1. 清除allResArray
  2. 从同一数组中的数据库再次加载数据
  3. notifyDataSetChanged