Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multiple listviews in a single activity on android?

Tags:

android

I have 9 listviews which should come under single activity. All the lists may/maynot display at a time on the screen (all at a time is nice)

like image 819
Anand Avatar asked Aug 04 '10 11:08

Anand


2 Answers

Here is a decent solution to more than one ListView within the same Activity. First the all-important layout file (copied from ANDROID : split the screen in 2 equals parts with 2 listviews , good job!). Note that the ListViews are embedded in their own LinearLayouts, which are each equally weighted.

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

                <ListView   android:id="@+id/list1" 
                            android:layout_height="fill_parent" 
                            android:layout_width="fill_parent">

                </ListView>
    </LinearLayout>

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

            <ListView   android:id="@+id/list2" 
                        android:layout_height="fill_parent" 
                        android:layout_width="fill_parent">

            </ListView>
</LinearLayout>

// Start java Code (you can figure out the imports)
public class AnActivity extends Activity {
private ListView lv1 = null;
private ListView lv2 = null;
private String s1[] = {"a", "b", "c", "d", "e", "f"};
private String s2[] = {"r", "s", "t", "u", "v", "w", "x"};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

        // Get UI references.
        //
    lv1 = (ListView) findViewById (R.id.list1);
    lv2 = (ListView) findViewById (R.id.list2);

    lv1.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, s1));
    lv2.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, s2));

} // onCreate()

} // class

Sorry if there are any typos. I'm editing right here in the window, not in eclipse. Good luck!

like image 103
SMBiggs Avatar answered Nov 14 '22 13:11

SMBiggs


@Scott many thanks, I found this post helpful. In my case I wanted the two listview to be on the same horizontal layout (sideways).

<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>
</LinearLayout>
like image 5
Mighty Avatar answered Nov 14 '22 13:11

Mighty