2

So I have a ListView of exercises, when I click an item i go to Detail.java activity and I display the informations of the exercise, but I have a button next in the bottom to go to the next exercise.i don't have any idea to do that without creating many activities of Detail.

Jonas
  • 121,568
  • 97
  • 310
  • 388
Hatim Setti
  • 115
  • 1
  • 9

3 Answers3

0

You can start Detail activity with a bundle with the parameters you want, in this example the id of the next exercise.

Intent mIntent = new Intent(this, Detail.class);
Bundle mBundle = new Bundle();
mBundle.putInt("id", theIdOfNextExercise);
mIntent.putExtras(mBundle);
startActivity(mIntent);

Then, in the launched Detail activity, you can read the parameter via:

int exerciseId = getIntent().getExtras().getInt("id", 0);

And then show the detail related to that exercise in your Detail Activity. I've set the default value as 0, so if exerciseId > 0 you have the id of the exercise to show.

Bundles have "get" and "put" methods for all primitive types, Serializables and Parcelables. The use of an integer with putInt/getInt is just an example.

jeprubio
  • 17,312
  • 5
  • 45
  • 56
0

you can use bundle to save the id of your exercise and than catch it to the activity you called... i suggest you to use fragment, it's more difficult but it's smartest and more user friendly

0

As far as i understand from the info u gave: You can pass object between activities using Serializable and Parcelable Interface that your object should implement

Using Serializable and Parcelable instructions

The next step passing data from your main activity to the detailed activity

Using serializable

ArrayList<ModelClass> yourModelList= new ArrayList<Model>();
intent.putExtra("modelList", yourModelList);

Retrieve data

ArrayList<ModelClass> modelList= (ArrayList<ModelClass>).getIntent().getSerializableExtra("modelList");

Using parcelable

Intent intent = new Intent(this,Detail.class);
intent.putParcelableArrayListExtra("modelList", modelList);
startActivity(intent);

Retrieve Data in your Detail.java activity

ArrayList<ModelClass> modelList= getIntent().getParcelableArrayList("modelList");

Besides I highly recommend you using Fragment, coz it gets messy once you start iterating through your arraylist from one activity to another saving ID's and other stuff Hope this helps ;)

Community
  • 1
  • 1
Aliy
  • 406
  • 6
  • 21
  • thank you it works, but getParcelableExtra("modelList") doesn't work i used getStringArrayListExtra.. – Hatim Setti Jan 02 '18 at 12:03
  • I have edited my answer try using .putParcelableArrayListExtra and getParcelableArrayListExtra – Aliy Jan 02 '18 at 15:04