1

In my xml file I have below list view.

<ListView
    android:id="@android:id/list"        
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="14dp"
    android:divider="@color/blue"
    android:dividerHeight="2dip"
    android:background="@drawable/list_divider"
    >
</ListView>

And in my java class I try to access the view as follows

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

I get a error for this saying "list cannot be resolved or is not a field". But if I change the id as android:id="@+id/list"

How can I access the view by using the id this way android:id="@android:id/list".

Also whats the basic difference between two ways of defining ID. Thanks in advance

asdlfkjlkj
  • 2,258
  • 6
  • 20
  • 28

3 Answers3

1

Assuming your Activity extends ListActivity, you can access it with the convenience method

ListView myListView = getListView();

otherwise you would access it with android.R.id.list

Also whats the basic difference between two ways of defining ID

  1. @android:id references built-in android resources
  2. @+id is a user defined id and will add it to the R.java file to be accessed later with @id

See this post about more on that

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
0

You have to mention your id like

android:id="@+id/list"

@+id/list will create a resource ID in your app (=your package) with the name "list" and give it a unique ID. In code, that would be R.id.list.

@android:id/list will use the ID "list" from the package android (which, in code, would be android.R.id.list.

PAC
  • 1,664
  • 1
  • 18
  • 24
0
  • Android resources id are referenced by @android:id/list in the xml and android.R.id.list in the Java code.

  • User defined id are referenced by @+id/list in the xml and R.id.list in the Java code. The + implies that the resource id value will be generated at compile time.

  • id define in resource files as <item type="id" name="list" /> will be referenced as @id/list in the xml and R.id.list in the Java code.

Olayinka
  • 2,813
  • 2
  • 25
  • 43