I wrote this method in My DataBaseHelper class:
public Cursor fetchData(String tableName) {
return myDataBase.rawQuery("SELECT rowid as _id, title FROM "+tableName, null);
}
And wrote this code inside one of my activities:
try {
Cursor cursor = myDbHelper.fetchData("tableName");
String[] columns = {cursor.getColumnName(0), cursor.getColumnName(1)};
int[] columnsLayouts = {R.id.layout1, R.id.layout2};
SimpleCursorAdapter ca = new SimpleCursorAdapter
(this.getBaseContext(),
android.R.id.list, cursor,columns , columnsLayouts);
lv.setAdapter(ca); //lv is my ListView with id="@android:id/list"
txt.setText("Done ! : "); //Process is Ok
} catch (Exception e){
txt.setText("Error"); //Error happens
}
The code is compiling ok. However, it closed forcefully when reaching this line lv.setAdapter(ca);
.
Is there a problem? did I do it the wrong way?
-
UPDATE:
this is the logCat errors (After I filter them by: android.view
)
Android crashes when trying to inflate the items in your list view. The second parameter of the constructor is layout -
layout - resource identifier of a layout file that defines the views for this list item. The layout file should include at least those named views defined in "to"
You are passing android.R.id.list which sounds to me like you are passing the list id instead of item's id.