5

我有一个带有 SimpleCursorAdapter 的 ListView,它从数据库加载信息,我希望 onlongclicklistener 从数据库中删除一个项目,它工作正常,但列表不会刷新,我尝试使用 listView.refreshDrawableState(); 以及我找到的一些其他方法作为答案,但没有一种方法有效,这是我的 onCreate 函数:

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_act);
    imzero = (ImageView)findViewById (R.id.iv_zero_pers);
    tvzero = (TextView)findViewById (R.id.tv_zero_pers);
    dbConnector = new DatabaseConnectorrso(MyListActivity.this);
    dbConnector.checkCursor();
    if (DatabaseConnector.de)//check if database is emplty
    {
    try{                
        dbConnector.open();             
        dbConnector.load();//load information from database
        dbConnector.close();
        imzero.setVisibility(View.GONE);
        tvzero.setVisibility(View.GONE);
    }
    catch(Exception ex) {
        ex.printStackTrace();
        dbConnector.close();
    }
    conAdapter =(new ImageAndTextAdapter(MyListActivity.this,
            R.layout.meniu_lista_element, null, null, null));
    setListAdapter(conAdapter);
    }
    else{
        dbConnector.close();
        imzero.setVisibility(View.VISIBLE);
        tvzero.setVisibility(View.VISIBLE);
    }

我试图像这样刷新列表

   listView.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            deleteDialog(Integer.parseInt (DatabaseConnector.id.get(position))); //delete from database 
            dbConnector.checkCursor();
            if (DatabaseConnector.de)
            {
            try{                
                dbConnector.open();             
                dbConnector.loadPersoane();
                listView.refreshDrawableState();
                dbConnector.close();
                imzero.setVisibility(View.GONE);
                tvzero.setVisibility(View.GONE);
            }
            catch(Exception ex) {
                ex.printStackTrace();
                dbConnector.close();
            }
            conAdapter =(new ImageAndTextAdapter(AltePersoane.this,
                    R.layout.meniu_lista_element, null, null, null));
            setListAdapter(conAdapter);
            }
            else{
                dbConnector.close();
                imzero.setVisibility(View.VISIBLE);
                tvzero.setVisibility(View.VISIBLE);
            }

            return true;
        }
    }); 
4

2 回答 2

1

尝试

yourAdapter.notifyDataSetChanged();

或者

yourAdapter.getCursor().requery();

更新

我认为你做错了什么,试试这个:

listView.setOnItemLongClickListener(new OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        deleteDialog(Integer.parseInt (DatabaseConnector.id.get(position))); //delete from database 
        dbConnector.checkCursor();
        if (DatabaseConnector.de)
        {
        try{                
            dbConnector.open();             
            dbConnector.loadPersoane();
                                //Removed
            dbConnector.close();
            imzero.setVisibility(View.GONE);
            tvzero.setVisibility(View.GONE);
        }
        catch(Exception ex) {
            ex.printStackTrace();
            dbConnector.close();
        }
        conAdapter.notifyDataSetChanged();
        }
        else{
            dbConnector.close();
            imzero.setVisibility(View.VISIBLE);
            tvzero.setVisibility(View.VISIBLE);
        }

        return true;
    }
}); 
于 2012-12-13T11:47:48.283 回答
-1

尝试将您的活动中的活动意图相同,它可以工作

    Intent i = new Intent (MyActivity.this,MyActivity.class);
    startActivity (i);
于 2012-12-13T14:24:06.570 回答