8

I have a list view declared in my xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:background="#F7EFE8">

    <ListView android:id="@+id/android:list"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"/>
        <TextView android:id="@+id/android:empty"
          android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/no_message"/>
</LinearLayout>

But when I try to declare/use it to java, the id is not found. But when i rename the list view to something else and try to use it i can see it "R.id._" but when R.id.list I can't find it. And If I didn't use the android:list there's an error that there should be a list view whose id is android:list. Any help here?

Kev
  • 173
  • 1
  • 4
  • 12
  • is there anything else included in your layout or maybe extra details you haven't mentioned? I tried that snippet you provided in a clean (with the xml header) layout file and it didn't work. – Alexandre Dec 07 '11 at 03:55

3 Answers3

28

Your ListView object id should be specified either as android:id="@android:id/list"

ListView lv = (ListView) findViewById(android.R.id.list);

or it should have some other id like android:id="@+id/sampleList"

ListView lv = (ListView) findViewById(R.id.sampleList);

Check this : ListActivity

and list

Hope this would help.

Kannan Suresh
  • 4,573
  • 3
  • 34
  • 59
3

In xml layout use,

 <ListView
   android:id="@+id/list"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"

 />

In Java Code:

 ListView listview=(ListView)findViewById(R.id.list);// it takes id of listview from xml

if you need to use android id for listview then replace your code as

<ListView
 android:id="@+android:id/list"
 android:layout_width="wrap_content"
android:layout_height="wrap_content" >

It may help you..

deepa
  • 2,496
  • 1
  • 26
  • 43
  • wat code u have used?did u use my first part of my answer? please post your error. – deepa Dec 07 '11 at 04:46
  • No need to add the `+` infront of the android id for the second part, because this id is already defined. This is probably why it wasn't working for OP – smac89 May 31 '15 at 07:54
0

Try this: android:id="@+id/list" Then clean project and rebuild. In java, just call findViewById like this:

   ListView lv = (ListView)findViewById(R.id.list); 


Ref: http://developer.android.com/reference/android/view/View.html

ductran
  • 10,043
  • 19
  • 82
  • 165