使用SimpleCursorAdapter时的运行时错误(IllegalArgumentException) - 列' _id'不存在

时间:2014-03-12 01:10:31

标签: java android sqlite

我尝试获取一些数据库值并将其显示在列表视图中。

这是我的代码:

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());
        }
    });
}


}

我得到了IllegalArgumentException

的RuntimeException

这是错误:

  

03-11 20:11:24.650:E / AndroidRuntime(887):java.lang.RuntimeException:
  无法开始活动
  ComponentInfo {com.NIRR.redistributionsystem / com.NIRR.redistributionsystem.SearchCustomer}:
  java.lang.IllegalArgumentException:column&#39; _id&#39;不存在

这是我的数据库查询:

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;
}

我使用的SQL查询在SQLite数据库浏览器上运行时给我正确的信息。 我不知道column '_id'是什么意思和#34;不退出&#34; 错误来自此行

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

请帮帮我 谢谢。

1 个答案:

答案 0 :(得分:2)

根据CursorAdapter的{​​{3}},其中SimpleCursorAdapter是子类:

  

Cursor必须包含名为“_id”的列,否则此类将无效。

如果您的表已经有一个名为“_id”的列,那么只需将其包含在查询的投影(请求的列)中。

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

如果你的表没有“_id”列,你可以改为默认所有SQLite表都具有的“rowid”(除了那些没有明确创建的那些)。

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

即使您没有使用原始SQL,也可以这样做。例如,在query()电话中:

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

当然,如果它等同于“_id”,您可以将另一个列替换为您可能已有的列。但请注意,如果您手动从中检索值,则必须使用别名(即“_id”)和Cursor