Search code examples
androidlistviewandroid-activity

start activity from ListView and then next activity


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.


Solution

  • 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 ;)