【问题标题】:android: Refresh ListView using ListAdapter and SimpleCursorAdapterandroid: 使用 ListAdapter 和 SimpleCursorAdapter 刷新 ListView
【发布时间】:2011-07-04 01:07:06
【问题描述】:

我正在尝试刷新使用作为 SimpleCursorAdapter 创建的 ListAdapter 的 ListView。

这是我在 onCreate 中创建 Cursor 和 ListAdapter 的代码,用于填充 ListView。

tCursor = db.getAllEntries();       

ListAdapter adapter=new SimpleCursorAdapter(this,
                R.layout.row, tCursor,
                new String[] columns,
                new int[] {R.id.rowid, R.id.date});

setListAdapter(adapter);

然后,我用另一种方法将一些数据添加到数据库中,但我不知道如何刷新 ListView。 stackoverflow 和其他地方的类似问题提到了使用 notifyDataSetChanged() 和 requery(),但 ListAdapter 或 SimpleCursorAdapter 的方法都不是。

【问题讨论】:

    标签: android listview refresh listadapter simplecursoradapter


    【解决方案1】:

    我可以通过创建一个新的适配器并再次调用 setListAdapter 来刷新 ListView。

    我在另一种方法中将其命名为adapter2。

    tCursor = db.updateQuery();       
    
    ListAdapter adapter2=new SimpleCursorAdapter(this,
                    R.layout.row, tCursor,
                    columns,
                    new int[] {R.id.rowid, R.id.date});
    
    setListAdapter(adapter2);
    

    我不知道为什么这是必要的,但它现在有效。如果有人有更好的解决方案,我愿意尝试。

    【讨论】:

    • 也解决了我的问题。我也可以在 AsynTask 中使用它。
    【解决方案2】:

    方法notifyDataSetChanged来自SimpleCursorAdapter父类BaseAdapter。父级实现ListAdapter,您应该可以将它传递给您的ListView。

    试试:

    tCursor = db.getAllEntries();       
    
    BaseAdapter adapter=new SimpleCursorAdapter(this,
                R.layout.row, tCursor,
                new String[] columns,
                new int[] {R.id.rowid, R.id.date});
    
    setListAdapter(adapter);
    


    那么你应该可以使用notifyDataSetChanged。

    【讨论】:

    • 我按照您的建议将适配器的类型从 ListAdapter 更改为 BaseAdapter。该列表仍然可以正确加载,但是当我从另一个方法调用它时,调用 adapter.notifyDataSetChanged() 不会刷新 ListView。我是不是用错了?
    【解决方案3】:

    在这种情况下,我建议通过扩展 BaseAdapter 类来自定义 Adapter。

    【讨论】:

    • 正如我在原始问题中提到的,notifyDataSetChanged() 不适用于 ListAdapter。 Eclipse 给出的错误是“ListAdapter 类型的 notifyDataSetChanged() 方法未定义”。
    • 重写有效的代码希望是最后的手段,但我会考虑到这一点。感谢您的尝试。
    【解决方案4】:

    如果需要从同一类中的其他方法访问适配器,可以将适配器定义为类变量。然后可以调用changeCursor()刷新ListView。

    public class mainActivity extends AppCompatActivity {
        // Define the Cursor variable here so it can be accessed from the entire class.
        private SimpleCursorAdapter adapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_coordinator_layout)
    
            // Get the initial cursor
            Cursor tCursor = db.getAllEntries();       
    
            // Setup the SimpleCursorAdapter.
            adapter = new SimpleCursorAdapter(this,
                R.layout.row,
                tCursor,
                new String[] { "column1", "column2" },
                new int[] { R.id.rowid, R.id.date },
                CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
    
            // Populate the ListAdapter.
            setListAdapter(adapter);
        }
    
        protected void updateListView() {
            // Get an updated cursor with any changes to the database.
            Cursor updatedCursor = db.getAllEntries();
    
            // Update the ListAdapter.
            adapter.changeCursor(updatedCursor);
        }
    }
    

    如果列表视图需要从另一个类中的方法更新,适配器变量应该声明为public static

    public static SimpleCursorAdapter adapter;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-05
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-12
      相关资源
      最近更新 更多