Android ListView数据已删除,数据已更新,已调用notifyDataSetChanged,但未更新我的列表

Android ListView数据已删除,数据已更新,已调用notifyDataSetChanged,但未更新我的列表,android,android-listview,Android,Android Listview,我有一个带有自定义适配器的ListView,它是从本地sqlite数据库填充的。当用户长时间单击项目并选择“删除”时,将弹出对话框再次确认操作,确认单击后项目将被删除。 我的问题是该项已从数据库中删除,并且arraylist(适配器的数据集)已更新,但即使我已调用notifyDataSetChanged(),ListView也未更新。 //删除方法在AlertDialog/PositiveButton click listener

我有一个带有自定义适配器的ListView,它是从本地sqlite数据库填充的。当用户长时间单击项目并选择“删除”时,将弹出对话框再次确认操作,确认单击后项目将被删除。 我的问题是该项已从数据库中删除,并且arraylist(适配器的数据集)已更新,但即使我已调用notifyDataSetChanged(),ListView也未更新。
//删除方法在AlertDialog/PositiveButton click listener 我已经检查或尝试了以下事情,但仍然。。。
一,。重建它(GoOnCreate),listview将正确显示,所以不应该是其他问题。
二,。我调用了相同的适配器,并用相同的哈希代码进行了确认
三,。setAdapter((ListViewAdapter)(ListViewer.mListView.getAdapter());
四,。allResArray.clear();loadData()

有人能帮我吗?谢谢我已经坚持了两天了

这是我的密码:

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();      
}
以及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();      
}
/**
*AlertDialog:删除数据前确认
*将其从ArrayList中删除,并将notifyDataSetChanged()移到适配器
*@param strListRID
*/
私有void showDeleteDialog(最终字符串mRID,最终整数位置){
AlertDialog.Builder dialog=新建AlertDialog.Builder(此);
对话框.setIcon(R.drawable.dialog\u通知);
setMessage(R.string.text_delete);
setPositiveButton(R.string.btn_delete,new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
if(delRes(Integer.parseInt(mRID)){
int size=allResArray.size();
对于(int i=0;i
您似乎已经从ArrayList获取了所有数据。在这种情况下,从数据库中删除条目时,您可以执行以下操作:

  • 清除allResArray
  • 再次从同一数组中的数据库加载数据
  • notifyDataSetChanged

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

    我也看到了同样的问题。删除备份存储区中的项并通过调用notifyDataSetChanged()通知适配器后,在适配器响应notifyDataSetChanged()进行工作时,似乎会在内部调用ListView.confirmCheckedPositionsById()正如您所指出的,一个解决方案是用一个新的适配器替换适配器,但正如您所指出的,这会弄乱用户在列表中的位置,并且用户必须再次向下滚动,这很糟糕

    因此,我所做的是检查我的派生适配器是否在保存数据的数组中超出范围,当超出范围时,只返回一个项目id 0。(请注意,我在此适配器中有稳定的ID,这可能会有所帮助)在这种情况下,我的备份存储是mTC,我在对检查项目ID的适配器的调用中执行此操作:

    类TaskAdapter扩展BaseAdapter实现OnClickListener{

    。。。 @凌驾 公共长getItemId(int位置){ //将来从Task.java中提取tode后更新此项 如果(位置>=mTC.size()) 返回0; 返回mTC.getTask(position.hashCode(); } ... }


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

    你能解释一下你在做什么吗?对于(inti=0;i /** * 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(); }