Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: id list view

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?

like image 731
Kev Avatar asked Dec 07 '11 03:12

Kev


People also ask

What is a list view in android?

Android ListView is a ViewGroup that is used to display the list of items in multiple rows and contains an adapter that automatically inserts the items into the list. The main purpose of the adapter is to fetch data from an array or database and insert each item that placed into the list for the desired result.

What is a list view?

Android ListView is a view which groups several items and display them in vertical scrollable list. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database.

Is ListView deprecated Android?

ListView Class (Android. Widget) | Microsoft Learn. This browser is no longer supported.

What is simple_list_item_1?

R. layout. simple_list_item_1 , which is a layout built into Android that provides standard appearance for text in a list, and an ArrayList called restaurants (not seen here).


2 Answers

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.

like image 190
Kannan Suresh Avatar answered Oct 26 '22 08:10

Kannan Suresh


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

like image 32
deepa Avatar answered Oct 26 '22 08:10

deepa