class extends ListActivity “whose id attribute is 'android.R.id.list'”
-
27-10-2019 - |
Question
I'm trying to extend my main Activity to "ListActivity" (previously extends to: Activity) to use onListItemClick, but the logcat, send me "Your content must-have a ListView whose id attribute is 'android.r.id.list'". (i'm using SQLite for populate the ListView).
mi xml is:
<ListView
android:id="@+id/list" // to "@id/android:list" or other styles (but nothing works)
android:layout_width="fill_parent"
android:layout_height="fill_parent"/> // or closing with </ListView>
this is the complete xml for the ListView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" /></LinearLayout>
My main class to handle the ListView:
public class lay_main extends ListActivity{
public ListView list;
private DBHelper myAdap;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lay_main);
collectXML();
setupDataBase();
setupAdapter();
}
private void collectXML()
{
list = (ListView)findViewById(R.id.list);
}
private void setupDataBase() {
myAdap = new DBHelper(getApplicationContext(), "courses", null, 1);
myAdap.insertCourses();
}
private void setupAdapter()
{
if(myAdap.getCourses()!=null)
{
cAdapter adapter = new cAdapter(this, R.layout.list_courses, myAdap.getCourses());
list.setAdapter(adapter);
}
}}
I read Android: Your content must have a ListView whose id attribute is android.R.id.list, or: RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list', and other questions, but don't works for me, what's going on here?
really would appreciate your help
Solution
When you want to extend ListActivity
you should use
android:id="@android:id/list"
and you will get your ListView
as
ListView listview = this.getListView();
or
ListView listview = (ListView)findViewById(android.R.id.list);
//consider android.R prefix
Here's the detailed explanation: How to use getListView() in Activity?