Hw.Master
Hw.Master

Reputation: 4452

Android use 2 listview linked together

I'm pretty new on android, i'm currently reading a lot of documentation to understand how to start. I've tried to search before asking here but i'm not able to find relevant information.

My problem is have 2 listview in the same acivity.

The Listview A with a list of category.

The Listview B with a details data (at startup the values showed are based on the category 1 of the listviewA)

When a click on a item of the listviewA the data of the listViewB change to reflect the new selection and show the new list of details data.

Can someone give me the right direction or a link to a tutorial that cover this topic ? Sorry i'm not able to post any code at the moment.

Upvotes: 1

Views: 790

Answers (3)

Nezam
Nezam

Reputation: 4132

I am not sure that the type of layout you are trying to make is user friendly. It would give the users far more richer UI experience if you change with an ExpandableListView. You can check out a tutorial here. Using this would give you the chances of engaging the user via an interaction.

enter image description here

Anyway, if you are sure to go with ListView then simply create a root LinearLayout and within this you take two ListViews. But taking the second one as a listview does not make sense. Rather, instead of making the second control a ListView, take a TextView to show the details.Provide the height of both layouts (ListView and TextView) according to your requirement.

Look at this tutorial

2 Listviews:

<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <ListView
                android:id="@+id/listView1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:visibility="visible">
            </ListView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" >

            <ListView
                android:id="@+id/listView2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:visibility="visible">
            </ListView>
        </LinearLayout>
    </LinearLayout>

1 ListView 1 TextView:

<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <ListView
                android:id="@+id/listView1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:visibility="visible">
            </ListView>
        </LinearLayout>
            <TextView android:id="@+id/TextView1" android:text="details"/>
    </LinearLayout>

UPDATE:

Please explain your problem a bit more. Because, in your scenario the data between the list views is connected with each other. Say, if ListView A is about Products so ListView B is about ProductDetails. So you need to declare a class variable say ProductID which is common entity between both data and set this variable setOnClickListener in ``getView()` of ListView A and fetch the data regarding this just after that and set the Adapter of ListView B with the results of this query (which may be to SQL db or RESTful server).

Upvotes: 1

AwadKab
AwadKab

Reputation: 3064

Try this:

After get listview by id (list_A) set click listener

list_A.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
              //here you can get click position from parameter position
             // and do somthing in Adapter_B and then call notifyDataSetChanged() for Adapter_B
              Adapter_B.notifyDataSetChanged(); 
            }
        });

Upvotes: 0

Elemental
Elemental

Reputation: 7521

I have used this strategy before and it worked out well for me. Use a linear layout to position the two list views, Populate the 'category' listview and set a list adaptor for it. On this listview I set an OnItemSelectedListener which when activated set ups the list-adaptor on the second 'detail' view.

Depending on your exact need you can either replace the detail listview's adapter or just change the content the adapter looks at and tell it to refresh.

Upvotes: 0

Related Questions