Search code examples
javaandroidandroid-intentandroid-listview

Notifying data change from one activity to another


I have a problem. I have two activities(MainActivity, EditAccount). In the MainActivity i compiled a listview from a database. And in the EditActivty, i updated data into the database and came back to MainActivity as soon as the data was changed with this:

    public class EditAccount extends Activity {
    ......
    ......


    public void update_acc(View view){

    EditText accName=(EditText)findViewById(R.id.acc_name);
    EditText comment=(EditText)findViewById(R.id.comments);
    String acc_name=accName.getText().toString();
    String comments=comment.getText().toString();
    Intent intent=getIntent();
    int id = (int) intent.getExtras().getLong("data_id"); 
    String data_id= Integer.valueOf(id).toString();

    datasource.updateEvent(acc_name, comments, data_id);
    datasource.close();

    Toast.makeText(getBaseContext(), "Account updated; Acc_name:"+acc_name+", Comment:"+comments, Toast.LENGTH_LONG).show();

    super.onBackPressed();
}

The problem is that when the Activity redirects back to MainActivity, the data is not updated there in the ListView. So, how can I notify the ListView that the data has been changed? Please help. Thanks in advance, Waiting for your reply...


Solution

  • The problem is that you are filling your ListView from database inside onCreate() possibly. So, solution is to fill the ListView with database inside onResume() that will fetch the updated data from the database.

    Because when you return back to MainActivity its onResume() will be called, so just put your code for fetching data from database inside onResume() instead of onCreate() and update your ListView.