Refresh a SimpleCursorAdapter after performing work on a non-UI thread

前端 未结 2 1603
旧巷少年郎
旧巷少年郎 2020-12-20 08:02

I\'m trying to call .notifyDataSetChange() on a SimpleCursorAdapter displayed in a ListView from an XML-parsing non-UI thread and can\'t for the life of me figure out how. I

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-20 08:14

    No need to create a new adapter...

    .notifyDataSetChanged() should be called only in case the data rows actually changed (inserted or deleted rows), in case you just updated the values on rows a simple call to requery() on your cursor should be enough:

    adapter.getCursor().requery();
    

    Edit: by your comment I see that you have in fact a compilation problem...

    You must declare the adapter as a class member (before/after mHandler declare it: private SimpleCursorAdapter adapter)

    Then when you initialize it, replace

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
        R.layout.directory_people_item, mCursor,
        new String[]{
            //snip
            new int[]{
            //snip
    ); 
    

    with:

    adapter = new SimpleCursorAdapter(this,
        R.layout.directory_people_item, mCursor,
        new String[]{
            //snip
            new int[]{
            //snip
    ); 
    

提交回复
热议问题