-2

I am trying to set two different adapters on two listviews in the same layout. After a few searches on the internet and this error "must have a listview whos id must be android.R.id.list", I have found out I need to define my listview id as @android:id/list.

What I wonder is how do I refer to two different listviews that are using @android:id/list?

Does the following code make sense?

ListView lv1 = (ListView) findViewById(android.R.id.list);
ListView lv2 = (ListView) findViewById(android.R.id.list);

ps: I am working in a Fragment and extending ListFragment.

Edoardo Moreni
  • 408
  • 2
  • 10
  • 24
  • give different id in xml file – Rustam Oct 21 '14 at 09:10
  • You must have to use custom adapter for that. In this case you no need to use this id, and can change name with any thing. But the question is, why you need it? – Pankaj Kumar Oct 21 '14 at 09:10
  • Take a look at this http://stackoverflow.com/questions/11770773/listfragment-layout-from-xml – Daniel M. Oct 21 '14 at 09:10
  • You have to define two difference id for two ListView and also check : http://stackoverflow.com/questions/4355614/what-is-difference-between-id-androidlist-and-id-list – Haresh Chhelana Oct 21 '14 at 09:10
  • why do you have two different ListView in the same fragment ? – Blackbelt Oct 21 '14 at 09:10
  • I am using two different adapters, but that's not the problem. I was wondering if with that code I was referring to the same listview, instead of the twos? In other words, how does android know that lv1 is different from lv2? – Edoardo Moreni Oct 21 '14 at 09:11
  • @blackbelt I need a listview, some text and then another listview to show other "stuff". – Edoardo Moreni Oct 21 '14 at 09:12
  • can't you have two different Fragment with one ListView? – Blackbelt Oct 21 '14 at 09:13
  • @blackbelt I didn't think about that, can't I just use two listviews in the same fragment? – Edoardo Moreni Oct 21 '14 at 09:14
  • 1
    `I am working in a Fragment and extending ListFragment.` THIS is the problem. You better extend Fragment (containing two ListViews, with different ids), instead of ListFragment. **OR** use 2 different ListFragments, each containing its own ListView. As you know, an Activity can contain multiple Fragments (or derivates). – Phantômaxx Oct 21 '14 at 09:14
  • you can't if you use a ListFragment – Blackbelt Oct 21 '14 at 09:17
  • @Funkystein Okay, I didn't know that, so if I extend fragment, then I can use two listviews? – Edoardo Moreni Oct 21 '14 at 09:17
  • **YES**. How many you want. This would be the easier and most practical solution. – Phantômaxx Oct 21 '14 at 09:18

3 Answers3

2

What I wonder is how do I refer to two different listviews that are using @android:id/list?

The simple answer is it's not possible (assuming both ListViews are in the same layout).

The id @android:id/list is simply a 'convenience' way of defining a resource id for a ListView when using a ListActivity or ListFragment.

The id of each widget in a layout must be unique which means you should define your own for the ListViews.

Squonk
  • 48,735
  • 19
  • 103
  • 135
1

in your xml file :

<ListView
        android:id="@+id/list1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
         >
    </ListView>
<ListView
        android:id="@+id/list2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
         >
    </ListView>

get reference of listview as:

ListView lv1 = (ListView) findViewById(R.id.list1);
ListView lv2 = (ListView) findViewById(R.id.list2);

Remember : R.id.list1 not android.R.id.list1

Rustam
  • 6,485
  • 1
  • 25
  • 25
0

In your xml define a custom id with: android:id="@+id/yourId" and reference the list with this new id

Juan Aguilar Guisado
  • 1,687
  • 1
  • 12
  • 21