ISURU ISURU - 29 days ago 5x
Android Question

Runtime error when using SimpleCursorAdapter

I try to get some db values and display them in a listview.

Here is my code:

public class SearchCustomer extends Activity{

private DBCreater dbHelper;

private SimpleCursorAdapter dataAdapter;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub

dbHelper = new DBCreater(this);;


private void displayListView(){

Cursor cursor = dbHelper.fetchallcustomerdata();
String[] columns = new String[] {

int[] to = new int[]{,,,,,

final ListView listView = (ListView)findViewById(;
dataAdapter = new SimpleCursorAdapter(this, R.layout.demosearch, cursor, columns, to,0);
listView.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> lidstView, View view, int position,
long id) {
// TODO Auto-generated method stub
Cursor cursor =(Cursor)listView.getItemAtPosition(position);
//Toast.makeText(cursor.getColumnIndexOrThrow(""), text, duration)
String name=cursor.getString(cursor.getColumnIndexOrThrow("customer_name"));
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();

EditText myFilter = (EditText)findViewById(;
myFilter.addTextChangedListener(new TextWatcher() {

public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub


public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub


public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub

dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {

public Cursor runQuery(CharSequence constraint) {
// TODO Auto-generated method stub
return dbHelper.fetchcustomerbyname(constraint.toString());


I get RuntimeException of

This is the error:

03-11 20:11:24.650: E/AndroidRuntime(887): java.lang.RuntimeException:

Unable to start activity


java.lang.IllegalArgumentException: column '_id' does not exist

This is my db query:

public Cursor fetchallcustomerdata() {
/*Cursor mCursor=ourDatabase.query(DATABASE_TABLE_CUSTOMER,new String[]{
String selectQuery = "select customer_name,customer_shop,customer_location,customer_phon,customer_email,customer_address from Customer" ;

SQLiteDatabase db = ourHelper.getReadableDatabase();
Cursor mCursor = db.rawQuery(selectQuery, null);
if(mCursor != null){
return mCursor;

the sql query that i use is give me the correct when it run on SQLite db browser.
I don't know what is the
column '_id'
that was say as "Does not exit"
The error come form this line

dataAdapter = new SimpleCursorAdapter(this, R.layout.demosearch, cursor, columns, to,0);

please help me
thank you.


Per the documentation for CursorAdapter, of which SimpleCursorAdapter is a subclass :

The Cursor must include a column named "_id" or this class will not work.

If your database already has a column named "_id", then just include this in the query's projection (the requested columns).

String selectQuery = "select _id, customer_name, ... from Customer";

If your database doesn't have an "_id" column, you can instead alias the "rowid" that all SQLite tables have by default (excepting those explicitly created without one).

String selectQuery = "select rowid as _id, customer_name, ... from Customer";

The same can be done even if you're not using raw SQL. For example, in a query() call:

mCursor = db.query("Customer", new String[] {"rowid as _id", "customer_name", ...}, ...);

You can, of course, alias another column you might already have, if it's equivalent to an "_id". Do note, though, that you must use the alias name with the Cursor, should you be manually retrieving values from it.