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!
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
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
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With