I have my main Activity with a fragment, which has a Recyclerview retrieving all the data populated from the database. In this fragment I got a button which calls another activity to insert an object into the database.
The problem is when the Activity ends and returns to the Main Activity the Recyclerview doesn't show the new item I just created. I tried with this piece of code:
@Override
public void onResume() {
super.onResume();
adapterItem.addItem(MyApplication.getWritableDatabase().getAllItems());
listProjectsView.scrollToPosition(0);
}
The function getAllItems retrieve all the data from sqlite.
On my adapterItem from my Recyclerview I got this:
public void addItem(ArrayList<Item> listItems) {
this.listItems = listItems;
notifyItemInserted(0);
}
It works "fine". The main problem is when I switch to another application in progress in my mobile and I return to my developed app the onResume method switches on and show again my last insert, populating twice this item (and so on if I do it again).
Thanks
To be able to scroll through a vertical list of items that is longer than the screen, you need to add a vertical scrollbar. Inside RecyclerView , add an android:scrollbars attribute set to vertical .
You can create static recyclerview (let say mRecyclerview) in the activity(let say firstActivity) which displays your recyclerview . And when you add new item to your list in the other activity(let say secondActivity), you can just use
firstActivity.mRecyclerview.getAdapter().notifyDataSetChanged();
I use this way for a while and didn't encounter any performance drawback but test it with your own code for sure.
Pass the new item back to MainActivity, then deal with it yourself.
1.In MainActivity, use startActivityForResult
to start the second Activity, like this:
startActivityForResult(new Intent(this, SecondActivity.class), REQUEST_CODE);
REQUEST_CODE
is an int
.
2.In second Activity, override finish
like this:
@Override
public void finish() {
Intent returnIntent = new Intent();
returnIntent.putExtra("passed_item", itemYouJustCreated);
// setResult(RESULT_OK);
setResult(RESULT_OK, returnIntent); //By not passing the intent in the result, the calling activity will get null data.
super.finish();
}
3.In MainActivity, override onActivityResult
like this:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
YourItem passedItem = data.getExtras().get("passed_item");
// deal with the item yourself
}
}
If it may help someone...
I had the same:
"I have my MainActivity with a fragment, which has a RecyclerView
retrieving all the data populated from the database. In this fragment, I got a button that calls another activity(insertA)
to insert an object into the database.
The problem was when the activity(insertA)
ends and returns to the Main Activity the RecyclerView
doesn't show the new item I just created."
So I used this in Fragment
button which calls another activity(insertA)
I just called
btn_addNew.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(context, insertA.class);
startActivity(myIntent);
}
});
WITHOUT FINISHING with finish();
!!!!!!!!!!!!
In insertA
Activity in the method AddNewThingToTheDataBase()
insted of finish();
or startActivity(Intent);
just put onBackPressed();
public void AddNewThingToTheDataBase(){
*****Inserting in DB code;
onBackPressed();
}
And in Main Activity add this code
@Override
public void onResume() {
super.onResume();
*YourUpdateRecyleViewFunction();*
}
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