My Listview Updates The First Change After Doing A Second One
Solution 1:
A better implementatiojn would be using a local database with for example the ROOM library.
A simpler approach to your problem, and better for mantainability would be using the startActivityForResult()
and onActivityResult()
in your main.
An example would be, in your AddToken.class
in the .onClick()
method add:
IntentresultIntent=newIntent();
resultIntent.putExtra("some_key", "String data");
setResult(Activity.RESULT_OK, resultIntent);
finish();
And in MainActivity.class
add the following:
//Inside the onClick() method in the onCreate()//...Intentintent=newIntent(MainActivity.this, AddToken.class);
startActivityForResult(intent, ANY_NOT_NEGATIVE_NUMBER);
//...//Add the following outside onCreate() as a new method@OverridepublicvoidonActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (ANY_NOT_NEGATIVE_NUMBER) : {
if (resultCode == Activity.RESULT_OK) {
// TODO Extract the data returned from the child Activity.StringreturnValue= data.getStringExtra("some_key");
}
break;
}
}
}
In this code ANY_NOT_NEGATIVE_NUMBER
is a number that you can define in your MainActivity.class
. Instead of having a shared reference, then use the putExtra()
method of the intent (in your AddToken.class
to pass the data back to MainActivity
and there add it to your list and notify the adapter of the change with notifyDataSetChanged()
on your Listview adapter.
I would suggest to use a recyclerview for better performance and functions. You can check it out HERE: Create dynamic lists with RecyclerView
Solution 2:
You added the item to the list, but didn't notify the adapter. The adapter has no way of knowing that the data changed, unless you notify it or recreate it
Post a Comment for "My Listview Updates The First Change After Doing A Second One"