更新sqlite时更新Listview

时间:2016-02-12 03:04:28

标签: android sqlite

大家好,我每次在数据库中添加项目时都会通知并更新列表视图吗?例如,我在一个活动中有两个片段然后当我从片段A发送数据库中的数据时,片段B中的列表视图也将更新而不使用接口。

1 个答案:

答案 0 :(得分:0)

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

如果遇到任何问题,请分享您的代码。

祝你好运=)