开发者

Android ListAdapter is not updating

I have a ListAdapter which is used to display a list in the Listview. Now I have added a longpress menu action for delete any selected item.

public boolean onContextItemSelected(MenuItem item) {

        AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item
                .getMenuInfo();
        final Long wordId = menuInfo.id;
        // selected_row = menuInfo.position;

        // To get the id of the clicked item in the list use menuInfo.id
        switch (item.getItemId()) {
        case CONTEXT_DELETE:
            deleteRes(wordId);  // delete function for the item
            break;
        default:
            return super.onContextItemSe开发者_JS百科lected(item);

        }
        //((BaseAdapter) favAdapter).notifyDataSetChanged();
        return true;
    }

But after deletion the list is updating and showing the old list with deleted item. I tried using notifyDataSetChanged(), but it is not working. What is the solution of the prob?


I used the following code and the problem is solved.

favCursor  = wordDataHelper.getCursorFav();
((SimpleCursorAdapter) favAdapter).changeCursor(favCursor);


delete the item from array/list, after that assign the array/list to adapter and after that write notifyDataSetChanged().


After you deleted the item from the list you have to get a new Cursor by doing a new query on the database. You can then change the cursor of your SimpleCursorAdapter (CursorAdapter) by calling changeCursor() with the new cursor as a parameter.


use getListView().invalidateViews after deletion.


  1. Delete the item from your database with a query
  2. Either get a new cursor, or requery the old one adapter.getCursor().requery()
  3. Call adapter.notifyDatasetChanged


Try using notifyDataSetChanged method, it should work.

adapter.notifyDataSetChanged();

But, sometimes it fails. If it fails for you, then reinitialize the adepter with the new list elements. It works for me.

adapter = new ArrayAdapter<Item>(getApplicationContext(),android.R.layout.simple_list_item_1, itemList);
setListAdapter(adapter);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜