ListView的数据删除和数据更新,notifyDataSetChanged调用,但没有更新我的清单 [英] ListView data deleted and data updated, notifyDataSetChanged called, but no updated of my list

查看:163
本文介绍了ListView的数据删除和数据更新,notifyDataSetChanged调用,但没有更新我的清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有自定义适配器一个ListView这是从本地SQLite数据库填充。当用户长按该项目,然后选择删除,对话框会弹出再次确认操作和项目将被删除,确定后点击。
我的问题是,该项目是从数据库和数组列表(数据集适配器)删除已更新,但在ListView不更新eventhough我已经叫notifyDataSetChanged()。结果
//删除方法设置在AlertDialog / PositiveButton点击收听LT; ListView控件的ContextMenu监听器。

我检查或尝试下面的东西,但仍...
结果1。重建它(去的onCreate),列表视图会显示正确的话,那么应该是没有其他问题。
结果2。我打电话给同一个适配器,都用相同的散列code证实
结果3。 mListView.setAdapter((ListViewAdapter)(ListViewer.mListView.getAdapter()));
结果4。 allResArray.clear(); loadData();

谁能帮助我吗?谢谢。我已经粘贴了2天!

下面是我的code:

 私有静态的ListView mListView;
私人ListViewAdapter mListViewAdapter;
公众的String [] strListRID;
公开名单<名单,LT;弦乐>> allResArray;公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setView(R.layout.listviewer);
    在里面();
}公共无效的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
私人无效initListView(){
    布尔SD_STATE = checkSDState();
    allResArray =新的ArrayList<名单,LT;弦乐>>();
    loadData();    mListView =(ListView控件)findViewById(R.id.ListView);
    mListViewAdapter =新ListViewAdapter(这一点,SD_STATE,allResArray);
    mListView.setAdapter(mListViewAdapter);
    Log.i(mListViewAdapter,将String.valueOf(mListViewAdapter.hash code()));    mListView.setCacheColorHint(00000000);    //点​​击监听器的ListView
    mListView.setOnItemClickListener(新OnItemClickListener(){
        @覆盖
        公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
            goIntent(5,strListRID [位置]);
        }
    });    //文本菜单监听器的ListView
    mListView.setOnCreateContextMenuListener(新OnCreateContextMenuListener(){
        @覆盖
        公共无效onCreateContextMenu(文本菜单菜单,查看视图,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);
        }
    });
}@覆盖
公共布尔onContextItemSelected(菜单项项){
    AdapterView.AdapterContextMenuInfo menuInfo =(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
    开关(item.getItemId()){
    案件0://编辑
        goIntent(5,strListRID [menuInfo.position]);
        返回true;
    案例1://删除
        showDeleteDialog(strListRID [menuInfo.position],menuInfo.position);
        打破;
    默认:
        打破;
    }
    返回super.onContextItemSelected(项目);
}私人无效loadData(){
    allResArray = getAllDatabaseRes();
    INT大小= allResArray.get(0).size();
    strListRID = allResArray.get(0).toArray(新的String [尺寸]);
}/ **
 * AlertDialog:确认之前删除数据
 *从ArrayList和notifyDataSetChanged()删除它适配器
 * @参数strListRID
 * /
私人无效showDeleteDialog(最后弦乐strListRID,最终诠释位置){
    AlertDialog.Builder对话框=新AlertDialog.Builder(本);
    dialog.setIcon(R.drawable.dialog_notice);
    dialog.setMessage(R.string.text_delete);
    dialog.setPositiveButton(R.string.btn_delete,新DialogInterface.OnClickListener(){
        公共无效的onClick(DialogInterface对话,诠释它){
            如果(delRes(的Integer.parseInt(strListRID))){
                                    INT大小=((ListViewAdapter)(mListView.getAdapter()))mResArray.size();
                的for(int i = 0; I<大小;我++){
                    ((ListViewAdapter)(mListView.getAdapter()))mResArray.get(ⅰ)上卸下摆臂(位);
                }
                ((ListViewAdapter)(ListViewer.mListView.getAdapter()))notifyDataSetChanged();
                Log.i(mListViewAdapter_check,将String.valueOf(((ListViewAdapter)(ListViewer.mListView.getAdapter()))的散列code()));
                showShortToast((字符串)getResources()的getText(R.string.text_record_delete_ok));
            }
            其他{
                showShortToast((字符串)getResources()的getText(R.string.text_record_delete_ng));
            }
        }
    });
    dialog.setNegativeButton(R.string.text_no,NULL);
    dialog.show();
}

delRes()code在BaseActivity:

  / **
 *删除数据库中的特定记录摆脱
 返回:布尔DEL_OK
 * @throws的SQLException
 * /
保护布尔delRes(INT strRID)抛出的SQLException {
    布尔DEL_OK = FALSE;
    字符串deleteWhere =RID =?;
    的String [] = deleteWhereValue {将String.valueOf(strRID)};
    Log.i(摆脱删除,deleteWhereValue [0]);    INT delrow = dbHelper.delete(表[0],deleteWhere,deleteWhereValue);
    Log.i(delrow,将String.valueOf(delrow));    如果(delrow == 1){
        DEL_OK =真;
    }
    返回DEL_OK;
}

而logcat的:

  15 02-09:19:59.744:I / mListViewAdapter(28235):1163110296
02-09 15:19:59.784:我的/ tmp(28235):/sdcard/Photo/20120208164204.jpg
02-09 15:19:59.814:我的/ tmp(28235):/sdcard/Photo/20120208163627.jpg
02-09 15:19:59.854:我的/ tmp(28235):/sdcard/Photo/20120209151917.jpg
02-09 15:20:05.514:I / menuInfo.position(28235):1
02-09 15:20:08.634:I /去掉删除(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:确认之前删除数据
 *从ArrayList和notifyDataSetChanged()删除它适配器
 * @参数strListRID
 * /
私人无效showDeleteDialog(最后弦乐MRID,最终诠释位置){
    AlertDialog.Builder对话框=新AlertDialog.Builder(本);
    dialog.setIcon(R.drawable.dialog_notice);
    dialog.setMessage(R.string.text_delete);
    dialog.setPositiveButton(R.string.btn_delete,新DialogInterface.OnClickListener(){
        公共无效的onClick(DialogInterface对话,诠释它){            如果(delRes(的Integer.parseInt(MRID))){
                INT大小= allResArray.size();
                的for(int i = 0; I<大小;我++){
                    allResArray.get(我)上卸下摆臂(位置);
                }
                strListRID = allResArray.get(0).toArray(新的String [大小1]);                //((ListViewAdapter)(mListView.getAdapter()))notifyDataSetChanged();
                mListView.setAdapter(新ListViewAdapter(getApplicationContext(),真的,allResArray));
                showShortToast((字符串)getResources()的getText(R.string.text_record_delete_ok));
            }
            其他{
                showShortToast((字符串)getResources()的getText(R.string.text_record_delete_ng));
            }
        }
    });
    dialog.setNegativeButton(R.string.text_no,NULL);
    dialog.show();
}


解决方案

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

I have a ListView with custom adapter which is populated from local sqlite database. When user long click the item and choose 'delete', dialog will popup to confirm the action again and item will be deleted after confirmed click. My problem is that the item was deleted from database and the arraylist( dataset for adapter ) was updated, but the ListView isn't updated eventhough I've already called the notifyDataSetChanged().
//The delete method is set on AlertDialog/PositiveButton click listener < ListView ContextMenu listener.

I've checked or tried the following things, but still...
1. Rebuild it (go onCreate), listview will show correctly then, so should be not other issue.
2. I called the same adapter, have confirmed by same hashCode
3. mListView.setAdapter((ListViewAdapter)(ListViewer.mListView.getAdapter()));
4. allResArray.clear(); loadData();

Can anyone help me please? Thanks. I've been stuck on it for 2 days!!

Here is my code:

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();      
}

delRes() code in BaseActivity:

 /**
 * 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;
}

And the 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

Current solution: Set a new 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();      
}

解决方案

Try deleting the row specified directly from your adapter, and then calling notifyDataSetChanged(). Or you can just create new adapter, iterate your ArrayList and adding data to your new adapter, then set this adapter to your ListView.

这篇关于ListView的数据删除和数据更新,notifyDataSetChanged调用,但没有更新我的清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆