0
嗨,大家好,我可以每次在數據庫中添加一個項目,它都會通知並更新列表視圖?例如,我在一個活動中有兩個片段,然後當我從片段A發送數據庫中的數據時,片段B中的Listview將更新而不使用接口。當sqlite更新時更新列表視圖
嗨,大家好,我可以每次在數據庫中添加一個項目,它都會通知並更新列表視圖?例如,我在一個活動中有兩個片段,然後當我從片段A發送數據庫中的數據時,片段B中的Listview將更新而不使用接口。當sqlite更新時更新列表視圖
有一個叫ArrayAdapter的功能 - notifyDataSetChanged()
//getting the list view
ListView userListView = (ListView) findViewById(R.id.users_ListView);
//creating an array to represnt what ever you want
ArrayList<String> users = new ArrayList<>();
for (int i = 0; i < 10 ; i++) {
//populate the array
String s = "user " + i;
users.add(s);
}
//setting up adapter to the array (this is 1 row with 3 arguments)
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,
users);
//connecting the list view to get the data to show from the adapter
userListView.setAdapter(adapter);
//changing the arrayList by adding or subtracting
String s = "another user";
users.add(s);
//update the adapter and list view with this command
adapter.notifyDataSetChanged();
分享您的代碼,如果您有任何麻煩。
好運=)
的可能的複製[如何刷新使用光標適配器列表視圖(http://stackoverflow.com/questions/20676701/how-to-refresh-the-listview-using-的光標適配器) – Rakesh