I am playing around with ListView in android and I have a simple class that extends ListActivity.
Whenever I change the name of the ListView in xml file from android:list to something else my program crashes.
Here is my ListActivity code:
private NewExpenseScreenModel mDbAdabter;
String[] items = { "Category", "Month", "Year"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.statistics);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter = new ArrayAdapter(
this,
android.R.layout.simple_spinner_item,
items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
mDbAdabter = new NewExpenseScreenModel(this);
populateFields();
}
private void populateFields()
{
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spending_row, mDbAdabter.getAllCategories());
setListAdapter(adapter);
}
and here is my xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/spinner_title"
/>
<ListView android:id="@+id/android:list" android:layout_height="wrap_content" android:layout_width="match_parent"></ListView>
</LinearLayout>
any idea what is going on..
I want to implement a onItemClickedListener later on so I want to be able to find the ListView by id.
I dont really understand why i cannot just change the id
Additional Info:
I also have another class that uses a different xml file with a list id=”android:list” but I never need to find that listView by id so its not a problem.
The other class uses the same xml file to describe layout of each row.
Update:
I managed to access the id by using findViewById(android.R.id.list); instead of findViewById(R.id.list); but i still dont understand why it wont let me rename the list
The list view id should be
@android:id/list.You use
+in id when you define a new id. Since you are using ListView id of the listview should havelistandlistid is already defined internally, so no need to add+.Edited
The above is mandatory when you extend the class from
ListActivity. See @Mejonzhan answer why so.Now, if you want your own id or when you want to have many List Views, then do not extend ListActivity instead extend Activity and handle the ListViews as below:
Change id as follows:
<ListView android:id="@+id/myListView"And in the code, access the list view as follows:
ListView myListView = (ListView) findViewById(R.id.myListView)