16

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)

Anand
  • 161
  • 1
  • 1
  • 3

3 Answers3

24

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!

Community
  • 1
  • 1
SMBiggs
  • 11,034
  • 6
  • 68
  • 83
  • 1
    How do I get the onListItemClick for each ListView like you do in ListActivity? – Totic May 11 '11 at 21:31
  • 1
    This is a GOOD question, because it pops up all the time and I've never seen it explained well. In the Activity class where you want to listen to a click (like onListItemClick), make sure that class implements OnClickListener. That's it, you can then @Override onListItemClick. Oh yeah, if you're target is lower than 2.1, you must omit the @Override part. Good luck! – SMBiggs May 19 '11 at 16:01
  • 1
    @Totic: I recommend making this a separate question. You can get complete answers then and other people will be able to benefit as well. – SMBiggs Jan 15 '12 at 07:19
  • @Totic: you could set the android:onClick in the xml layout files. – Arthulia Oct 28 '13 at 22:42
5

@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>
antonv
  • 1,227
  • 13
  • 21
Mighty
  • 51
  • 1
  • 3
0

You should use page viewer, take a look at this example, hope this will help you http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/

Stephen
  • 1,737
  • 2
  • 26
  • 37
Osama.070032
  • 158
  • 1
  • 2
  • 10
  • 5
    Rather than just adding a link, put some detail here so people can make use of your answer without having to visit some site, use it as a reference – Stephen Jan 31 '12 at 08:51