0

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;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_customer);

    dbHelper = new DBCreater(this);
    dbHelper.open();

    displayListView();
}

private void displayListView(){

    Cursor cursor = dbHelper.fetchallcustomerdata();
    String[] columns = new String[] {
        DBCreater.Key_customer_Name,
        DBCreater.Key_customer_Shop,
        DBCreater.Key_customer_Location,
        DBCreater.Key_customer_Phon,
        DBCreater.Key_customer_Email,
        DBCreater.Key_customer_Address
    };

    int[] to = new int[]{
            R.id.tv_I_name_demo,
            R.id.tv_I_shop_demo,
            R.id.tv_I_location_demo,
            R.id.tv_I_phone_demo,
            R.id.tv_I_email_demo,
            R.id.tv_I_address_demo
    };

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

        @Override
        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(R.id.et_I_filter_searchCustomer);
    myFilter.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            dataAdapter.getFilter().filter(s.toString());

        }

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

        }

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

        }
    });
    dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {

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


}

I get RuntimeException of IllegalArgumentException

This is the error:

03-11 20:11:24.650: E/AndroidRuntime(887): java.lang.RuntimeException:
Unable to start activity
ComponentInfo{com.NIRR.redistributionsystem/com.NIRR.redistributionsystem.SearchCustomer}:
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[]{
            Key_customer_Name,Key_customer_Shop,Key_customer_Location,Key_customer_Phon,
            Key_customer_Email,Key_customer_Address},null,null,null,null,null,null);*/
     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){
        mCursor.moveToFirst();
    }
    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.

1 Answer 1

2

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 table 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 table 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 - i.e., "_id" - with the Cursor, should you be manually retrieving values from it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.