Dennis
Dennis

Reputation: 2311

Refresh ListView after updating in another Activity

I have two simple Activities - Activity1: ListView from an Array Activity2: EditText for editing the clicked row in Activity1

When I edit the value in Activity2 and returning to Activity1, the ListView doesn't reload the new value.

I want to refresh the ListView when I return from Activity2 or resume Activity1 or something that will update the list.

My code:

static ArrayAdapter<String> dataAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            // Loading the values to list array
        String[][] fulllist = loadArrays();
        String[] list = new String[fulllist.length];
        for(int i = 0; i<fulllist.length; i++) {
            list[i] =  fulllist[i][1];
            }
            // --------------------------------

            dataAdapter = new ArrayAdapter<String>(this,
                                android.R.layout.simple_list_item_1,
                                list);
            setListAdapter(dataAdapter);
            }
@Override
public void onResume() {
        super.onResume();
        // Loading the values to list array
        String[][] fulllist = loadArrays();
        String[] list = new String[fulllist.length];
        for(int i = 0; i<fulllist.length; i++) {
            list[i] =  fulllist[i][1];
            }
        // --------------------------------

        dataAdapter = new ArrayAdapter<String>(this,
                            android.R.layout.simple_list_item_1,
                            list);
        dataAdapter.notifyDataSetChanged();
}

loadArrays() is just method that converts from SharedPreferences to String Array. Activity2 saves the new data in SharedPreferences and than Activity1 can read it (with the new data). If I return to the "main activity" (it is not Activity1) and than come back to Activity1 - the new data is shown, but I want this data will be updated when I return from Activity2 immediately.

loadArrays() method: pastebin.com/MHwNC0jK

Thanking you in advance!

Upvotes: 6

Views: 16221

Answers (3)

Sudarshan Bhat
Sudarshan Bhat

Reputation: 3772

On clicking the item in your first Activity, start your second Activity with startActivityForResult()

And then in Second Activity, after entering in EditText, probably there is a button. And in onClick of that button call,

intent.putExtra("edittextvalue", findViewById(R.id.edittext).getText().toString());
setResult(RESULT_OK, intent);
finish();

Now you come back to your first Activity and here you have to implement onActivityResult() callback. You can extract data from that intent's extras and set that respective item in your array and call notifyDataSetChanged().

This is ideally how you should be doing it.

If you want more info on how to use startActivityForResult() try this link - http://manisivapuram.blogspot.in/2011/06/how-to-use-startactivityforresult.html

Upvotes: 14

kumar
kumar

Reputation: 691

1) Get reference ListView

mListView = (ListView)findViewById(R.id.auto_listview);

2) Create adapter One more time with changed values

MyAdapter myAdapter = new MyAdapter(getApplicationContext(), 
                                    R.layout.locations_list_item_layout,dataArray;
mListView.setAdapter(myAdapter);

setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                public void onItemSelected(AdapterView<?> adapterView,
                        View view, int i, long l) {

myAdapter = new MyAdapter(getApplicationContext(), 
    //pass changed values vlues array                               R.layout.locations_list_item_layout,dataArray;
mListView.setAdapter(myAdapter);

}

                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub

                }
            });

Upvotes: 1

Hemantvc
Hemantvc

Reputation: 2119

Declare fulllist as Globle Variable and used static arraylist .

Upvotes: -3

Related Questions