从数据库中删除后刷新ListView

时间:2012-12-13 11:39:59

标签: android database listview

我有一个带有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;
        }
    }); 

2 个答案:

答案 0 :(得分: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;
    }
}); 

答案 1 :(得分:-1)

尝试将活动中的活动意图转移到相同的活动,它可以正常工作

    Intent i = new Intent (MyActivity.this,MyActivity.class);
    startActivity (i);