ListView在返回活动时不会更新

时间:2013-02-14 06:54:12

标签: android android-listview listviewitem

我正在开发一个应用程序,我在一个屏幕上有一个列表视图,因为我使用类传递值。在listview项目点击事件中,它会打开选​​择值的活动。我将此值保存到SharedPrefrence,因为我希望这些数据到另一个活动。它将值存储到SharedPreference。但它在返回活动时不会更新listview。但我正在更新列表onCreate()

try{
            selectedParentMessage = ParentMessageListActivity.parentMsgSharedPref.getString("SelectedParentMessage", "None");

            System.out.println("Selected Parent Message:"+selectedParentMessage);

        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();

        }

        items.add(new Setting_ActivitySectionItem("Sounds"));
        //items.add(new Setting_ActivityEntryItem("Cartoon's Voice", ""));
        items.add(new Setting_ActivityEntryItem("Record Parent's Message", ""+selectedParentMessage));
        items.add(new Setting_ActivityEntryItem("Import Lullaby", ""));


        items.add(new Setting_ActivitySectionItem("Alerts"));
        items.add(new Setting_ActivityEntryItem("Phone Number", ""));
        items.add(new Setting_ActivityEntryItem("Email Id", ""));
        items.add(new Setting_ActivityEntryItem("Send Notification", ""));
        //items.add(new EntryItem("Item 7", "This is item 2.4"));

        items.add(new Setting_ActivitySectionItem("Baby's Noise Level"));
        items.add(new Setting_ActivityEntryItem("High", ""));
        items.add(new Setting_ActivityEntryItem("Medium", ""));
        items.add(new Setting_ActivityEntryItem("Low", ""));
        //items.add(new EntryItem("Item 11", "This is item 3.4"));
        //items.add(new EntryItem("Item 12", "This is item 3.5"));

        items.add(new Setting_ActivitySectionItem("Set Auto-silent"));
        items.add(new Setting_ActivityEntryItem("Auto Silent", ""));

        items.add(new Setting_ActivitySectionItem("Set Battery reminder"));
        items.add(new Setting_ActivityEntryItem("Battery Reminder", ""));

        adapter = new Setting_ActivityEntryAdapter(this, items);
        adapter.notifyDataSetChanged();
        setListAdapter(adapter);

我也在notifydatasetchange()onResume

上应用onRestart()
public void onRestart(){
        super.onRestart();
        System.out.println("onrestart");
        try{
            selectedParentMessage = ParentMessageListActivity.parentMsgSharedPref.getString("SelectedParentMessage", "None");

            System.out.println("Selected Parent Message:"+selectedParentMessage);

        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();

        }
        getListView().invalidateViews();
        adapter.notifyDataSetChanged();
    }

onPause()

相同

4 个答案:

答案 0 :(得分:2)

已解决

@Override
    public void onResume(){
    super.onResume();
        adapter.clear();
        addItem();
    }

addItem()是将列表项添加到类并生成适配器并设置列表视图的函数。

答案 1 :(得分:1)

如何使用您在适配器中使用的相同变量创建适配器的新实例,并再次在列表中设置适配器。

在onResume上试试这个:

adapter = new Setting_ActivityEntryAdapter(this, items);
setListAdapter(adapter);

答案 2 :(得分:0)

称之为yourlistView.invalidateViews();

试试这个。

ListAdapter la = listView.getAdapter();
                        if( la != null){
                            ((BaseAdapter)la).notifyDataSetChanged();


                }

答案 3 :(得分:0)

notifydatasetchange()

中适用listview的适配器onStart()