2016-02-12 80 views
0

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

+0

的可能的复制[如何刷新使用光标适配器列表视图(http://stackoverflow.com/questions/20676701/how-to-refresh-the-listview-using-的光标适配器) – Rakesh

回答

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

分享您的代码,如果您有任何麻烦。

好运=)