Dzianis
Dzianis

Reputation: 63

How to populate a ListView based on the action in previous activity?

Currently there are three buttons in my main activity. Each button opens a new activity with a ListView containing different photos. Each ListView has a different size.

I've created a schema of my question:schema

My question is: How to create only one activity that contains a ListView and populate it with different photos depending on which button is clicked?

I suppose I have to use intent.putExtra to define button number and use this number in if statement to add images to ListView. But I'm not sure in this solution.

Upvotes: 1

Views: 67

Answers (3)

user8959091
user8959091

Reputation:

Why aren't you sure about this solution?
If the layout of the activity to be opened is the same in all 3 cases and the only difference is the contents of the list, then it is the simplest and best solution.
Just pass an integer value to the intent and check it in the onCreate() of the activity that is opened. Depending on the value create and set the adapter for the list and you' re done.
This is the most efficient way and less resources consuming.

Upvotes: 3

praveen2034
praveen2034

Reputation: 119

Create 1 custom adapter which takes Arraylist of intergers(i.e Resource id's) and set those id to imageview in adapter.

in activity onclick of button add the related id's to Arraylist and pass via intent to next activit. You can pass and get arraylist from intent like below

1) intent.putStringArrayListExtra("array_list", array_list); 2) intent.getStringArrayListExtra("array_list");

Upvotes: 1

Tomas Jablonskis
Tomas Jablonskis

Reputation: 4386

Create 3 Fragments and on each button press just load required Fragment. Each of those Fragments should have ListView/RecyclerView in them and use different data sources. That is it :) This, of course, can be implemented in several ways, but that is how I would approach this.


Good luck :)

Upvotes: 3

Related Questions