I am having problem with the flow of my code. Please please please help :(
Okay, so the flow goes like this. the first Activity loads the recyclerview
which stores the ArrayList
data. The data of the Arraylist
consist of information about a Premise
which it retrieve from an API
of a website. It uses GET
method.
Okay, next when I select an item from the recyclerview
, it brings me to the next Activity
which I will enter an integer
. Then, as it returns to the first Activity
, it passes the integer
value and the index
of the recyclerview
item. The recyclerview
updates the item with the index
and stores the integer
value in the item.
So, my problem is: when I repeat the process of selecting an item from the recyclerview
and enter an integer, the first activity only stores the latest data. the previous item is gone.
Where on Earth did I did wrong? I am new to mobile app development. Please help. I am beyond clueless.
Here's the first Activity. It's called MainActivity:
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<ArrayList<Premise>> {
private NotificationManagerCompat notificationManager;
private LoaderManager loaderManager;
private RecyclerView rcvPremises;
private EditText premiseSearch;
String pTrackVisitor_s, pPosition_s, pPremiseName, pLocation;
int pTrackVisitor, pPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loaderManager = LoaderManager.getInstance(this);
premiseSearch = findViewById(R.id.search);
rcvPremises = findViewById(R.id.rcvPremise);
Intent mainIntent = getIntent();
pTrackVisitor_s = mainIntent.getStringExtra("trackVisitor");
pPosition_s = mainIntent.getStringExtra("position");
if (pTrackVisitor_s != null) {
pTrackVisitor = Integer.parseInt(pTrackVisitor_s);
pPosition = Integer.parseInt(pPosition_s);
} else {
pTrackVisitor = 0;
pPosition = 0;
}
}
@NonNull
@Override
public Loader<ArrayList<Premise>> onCreateLoader(int id, @Nullable Bundle args) {
return new PremisesListTaskLoader(this, pTrackVisitor, pPosition);
}
@Override
public void onLoadFinished(@NonNull Loader<ArrayList<Premise>> loader, ArrayList<Premise> vPremise) {
loaderManager.destroyLoader(0);
if (!vPremise.isEmpty()) {
rcvPremises.setAdapter(new PremiseAdapter(this, vPremise));
rcvPremises.setLayoutManager(new LinearLayoutManager(this));
premiseSearchBar(premiseSearch, vPremise);
} else
Toast.makeText(this, "Unable to load data from iiotisme.com", Toast.LENGTH_LONG).show();
}
@Override
public void onLoaderReset(@NonNull Loader<ArrayList<Premise>> loader) {
}
}
Please let me know if you need more parts of the coding. I 'll share them.
First of all, you should not read data in onCreate
, it will be only triggered when Activity is created first time, I assume your activity will be resume, it should use something like onActivityResult
. You should start second activity using startActivityForResult
and in onActivityResult
, you will get an intent
from where you can read the latest data every time.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data == null)
return;
pTrackVisitor_s = data.getStringExtra("trackVisitor");
pPosition_s = data.getStringExtra("position");
if (pTrackVisitor_s != null) {
pTrackVisitor = Integer.parseInt(pTrackVisitor_s);
pPosition = Integer.parseInt(pPosition_s);
} else {
pTrackVisitor = 0;
pPosition = 0;
}
}
You can pass the index
and other required data to your second activity using intent
like below
Intent data = new Intent();
data.putExtra("position", <yourClickedIndex>);
startActivityForResult(1234, SecondActivity.class);