[SOLVED] Android use 2 listview linked together


This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under
CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

Issue

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.

Solution

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).

Answered By – Nezam

people found this article helpful. What about you?