简体   繁体   English

android:使用ListAdapter和SimpleCursorAdapter刷新ListView

[英]android: Refresh ListView using ListAdapter and SimpleCursorAdapter

I'm trying to refresh a ListView that uses a ListAdapter created as a SimpleCursorAdapter. 我正在尝试刷新使用创建为SimpleCursorAdapter的ListAdapter的ListView。

Here is my code for creating the Cursor and ListAdapter in onCreate which populates the 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);

Then, I add some data to the db in another method, but I can't figure out how to refresh the ListView. 然后,我在另一个方法中向db添加一些数据,但我无法弄清楚如何刷新ListView。 Similar questions on stackoverflow and other places mention using notifyDataSetChanged() and requery(), but neither are methods of ListAdapter or SimpleCursorAdapter. stackoverflow和其他地方的类似问题提到使用notifyDataSetChanged()和requery(),但ListAdapter或SimpleCursorAdapter的方法都没有。

I'm able to get the ListView to refresh by creating a new adapter and calling setListAdapter again. 我可以通过创建一个新的适配器并再次调用setListAdapter来刷新ListView。

I named it adapter2 in the other method. 我在另一种方法中将它命名为adapter2。

tCursor = db.updateQuery();       

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

setListAdapter(adapter2);

I'm not sure why this is necessary, but it works for now. 我不确定为什么这是必要的,但它现在有效。 If anyone has a better solution, I'm willing to try it. 如果有人有更好的解决方案,我愿意尝试。

You can define the adapter as a class variable if it needs to be accessed from other methods in the same class. 如果需要从同一类中的其他方法访问适配器,则可以将适配器定义为类变量。 Then you can call changeCursor() to refresh the ListView. 然后,您可以调用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);
    }
}

If the list view needs to be updated from methods in another class, the adapter variable should be declared public static instead 如果需要从另一个类中的方法更新列表视图,则应将适配器变量声明为public static

public static SimpleCursorAdapter adapter;

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

The method notifyDataSetChanged comes from the SimpleCursorAdapter parent class BaseAdapter . notifyDataSetChanged方法来自SimpleCursorAdapter父类BaseAdapter The parent implements ListAdapter and you should be able to pass it to your ListView . 父实现ListAdapter ,您应该能够将它传递给ListView

Try: 尝试:

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);


Then you should be able to use notifyDataSetChanged . 然后你应该能够使用notifyDataSetChanged

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM