Saurabh
Saurabh

Reputation: 457

ListView doesn't update on returning to the activity

I am working on one app and I have one list view in one screen in that i am passing values using class. On listview's item click event it opens activity for selecting values. and i m saving this value to the SharedPrefrence as i wanted these data to the another activity. It stores value to the SharedPreference. But it doesnt update listview on returning to the activity. But I am getting updated List 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);

and also i am applying notifydatasetchange() on onResume and 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();
    }

and same for onPause()

Upvotes: 2

Views: 1399

Answers (4)

Saurabh
Saurabh

Reputation: 457

SOLVED

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

addItem() is function which will add list items to the class and generate the adapter and set the listview.

Upvotes: 2

lorraine batol
lorraine batol

Reputation: 6281

How about creating a new instance of the adapter using the same variable you used in your adapter and set the adapter on the list again.

Try this on onResume:

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

Upvotes: 1

kumar_android
kumar_android

Reputation: 2263

Do notifydatasetchange() for your adapter of listview in onStart()

Upvotes: 0

Asad Iqbal
Asad Iqbal

Reputation: 304

call it yourlistView.invalidateViews();

Try this .

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


                }

Upvotes: 0

Related Questions