4

我正在显示从 Android OS sqlite 数据库中提取的数据。当我单击它们时,我成功地删除了要删除的项目。但是,我在操作后刷新或更新列表视图时遇到问题。

下面是我删除联系人的代码。

deleteBtn = (Button)v.findViewById(R.id.deleteBtn);
    deleteBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(v.getContext(), "Deleted: " + c.getId() + " " + c.getName(), Toast.LENGTH_SHORT).show();
            adapter.deleteContact(c.getId());
            updateList();
        }
    });

下面是 updateList() 方法:

public void updateList(){
    myList.refreshDrawableState();
    myList.invalidateViews();
    this.notifyDataSetChanged();
}

我在此方法中包含了我尝试刷新的所有三种方式,但没有一种对我有用。知道如何实现这一目标吗?

编辑:我改变了我的代码,认为这将是解决方案,但它也不起作用:

DbAdapter 类删除方法():

public boolean deleteContact(int rowId){
    getAllContactsList();
    return db.delete(DB_TABLE, COLUMN_ID + "=" + rowId, null) > 0;

}

获取所有联系人列表():

public List<Contact> getAllContactsList(){
    List<Contact> contactList = new ArrayList();

    Cursor c = db.query(DB_TABLE, new String [] {COLUMN_ID, COLUMN_FNAME, COLUMN_LNAME}, null, null, null, null, null);

    //loop through cursor rows and add to list
    if(c.moveToFirst()){
        do{
            Contact contact = new Contact();
            contact.setId(Integer.parseInt(c.getString(0)));
            contact.setfName(c.getString(1));
            contact.setlName(c.getString(2));
            contactList.add(contact);
        }while(c.moveToNext());
    }
    return contactList;
}public List<Contact> getAllContactsList(){
    List<Contact> contactList = new ArrayList();

    Cursor c = db.query(DB_TABLE, new String [] {COLUMN_ID, COLUMN_FNAME, COLUMN_LNAME}, null, null, null, null, null);

    //loop through cursor rows and add to list
    if(c.moveToFirst()){
        do{
            Contact contact = new Contact();
            contact.setId(Integer.parseInt(c.getString(0)));
            contact.setfName(c.getString(1));
            contact.setlName(c.getString(2));
            contactList.add(contact);
        }while(c.moveToNext());
    }
    return contactList;
}

我想通过在删除联系人之前获取一个新光标它会相应地更新列表。不幸的是,它没有任何区别。有任何想法吗 ?

4

3 回答 3

0

It seems that you have the Database Adapter class, but you're missing the ArrayAdapter class, which is intended to manage the list of items, displayed in your ListView. Take a look at this example, specifically at the WeatherAdapter.java class.

If I am wrong in my assumptions and the adapter object in your code is not a database adapter, but an ArrayAdapter class, try putting adapter.notifyDataSetChanged() instead of this.notifyDataSetChanged().

Let me know if this helped.

于 2013-01-15T00:09:27.610 回答
0

您必须通知列表的适配器您已修改基础数据。

尝试使用adapter.notifyDataSetChanged();

于 2013-01-14T23:14:50.987 回答
0

您必须重新查询并生成新列表:

public void updateList(){
    clear();
    addAll(contactsDbHelper.getAllContactsList()); //addAll works since 11 API version.
    notifyDataSetChanged(); //need this is you dissabled auto notify
}

PS 您应该使用Content providersCursorAdapter来完成这项工作,但是您需要手动通知内容提供者有关更改,因为Cursor.requery()自 11 版本以来已弃用。

于 2013-01-14T23:16:06.243 回答