A
A
Anton @ Lyalin2017-09-25 13:01:44
Android
Anton @ Lyalin, 2017-09-25 13:01:44

How to use one activity with one ListView for many array?

There is a ListView1, on click of which a second Activity appears with one ListView2. How can I pass different arrays to this LIstView2? That is, I clicked on item1, got one array, clicked on item2, got the second array.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Myvrenik, 2017-09-25
@gim0

Before calling startActivity, add data to the Intent with one of these methods (depending on what type of value you need to pass):
Intent#putIntegerArrayListExtra
Intent#putStringArrayListExtra
Intent#putParcelableArrayListExtra
Then in the second Activity you get the passed data by keys:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        // Достаёшь и используешь данные из extras
    }
}

See Intents and Intent Filters

S
Sergey, 2017-09-26
@red-barbarian

Normally, you need to separate the view layer and the data layer. At least it's primitive.
Those. make Activity1, Actyvity2 and some class that will provide data. For example, let's call provider.
Outcome.
Activity1 is filled with the provider.getList1() list
when item 1 is selected, calls Actyvity2, throwing item1 through invent
When Activity2 starts, we pull item1 from the intent, and fill the listview with the provider.getList2(item1) list,
the idea is to activate separately, lists separately.
You can also consider not launching an activity, but using fragments. But of course, if the task allows.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question