wervdon
wervdon

Reputation: 557

How to show a ListView inside another ListView after Click

I have a ListView:
A
B
C
D

I am trying to show a second ListView once an item is selected from the first. I tried the following:
A b1
B b2
C b3
D

It's working but I didn't like the look of it. How can I implement the list in such a way that the second ListView (b1-b2-b3) shows up under the selected item by moving the rest below:
A
B
b1
b2
b3
C
D

Also I have another question, how can I introduce a new String array depending on the selected item. Currently I am using a switch clause but it will be problematic if I happen to need more arrays:

String[] brands = { ... };    String[] test2 = { ... };

    ArrayAdapter<String> arrayAdapterOne = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, brands);
    final ArrayAdapter<String> arrayAdapterTwo = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, test2);
    final ArrayAdapter<String> arrayAdapterThree = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, test3);
    final ArrayAdapter<String> arrayAdapterFour = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, test4);


listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
            TextView tv1 = (TextView) findViewById(R.id.textView1);
            tv1.setText(brands[position]);
            switch (position) {
            case 0:
                listView2.setAdapter(arrayAdapterTwo);
                break;
            case 1:
                listView2.setAdapter(arrayAdapterThree);
                break;
            case 2:
                listView2.setAdapter(arrayAdapterFour);
                break;
            case 3:
                listView2.setAdapter(arrayAdapterFour);
                break;

            }
        }
    });

Upvotes: 1

Views: 1035

Answers (3)

bazyle
bazyle

Reputation: 776

Maybe you want to try the ExpendableListView. It should look something like these patterns.

Hope it helps!

Upvotes: 2

Opiatefuchs
Opiatefuchs

Reputation: 9870

First Question: I Think what You want is an ExpandableListView...look at this tutorial:

http://www.dreamincode.net/forums/topic/270612-how-to-get-started-with-expandablelistview/

Upvotes: 1

Nickolaus
Nickolaus

Reputation: 4835

I don't think thats possible (had nearly the same issue), I would recommend using an ExpandalbeListView instead.
This should provide all (or nearly similar) features you are trying to implement

Upvotes: 2

Related Questions