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.