小编典典

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

java

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

这是我的代码:

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


}

我得到的RuntimeException IllegalArgumentException

这是错误:

03-11 20:11:24.650:E / AndroidRuntime(887):java.lang.RuntimeException:
无法启动活动
ComponentInfo {com.NIRR.redistributionsystem /
com.NIRR.redistributionsystem.SearchCustomer}:
java.lang.IllegalArgumentException:列’_id’不存在

这是我的数据库查询:

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 db浏览器上运行时会给我正确的查询。我不知道这是什么意思column '_id',因为“不退出”这一行来了

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

请帮我谢谢。


阅读 272

收藏
2020-11-30

共1个答案

小编典典

按照文档CursorAdapter,它的SimpleCursorAdapter一个子类:

游标必须包含一个名为“ _id”的列,否则此类将无法工作。

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

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

如果您的表中没有“ _id”列,则可以对所有SQLite表默认具有的“行”进行别名(除非明确创建了没有行的列)。

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

即使您不使用原始SQL,也可以执行相同的操作。例如,在query()通话中:

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

如果等效于“ _id”,则当然可以为另一列添加别名。不过请注意,Cursor如果您要手动从中检索值,则必须将别名(即“ _id”)与一起使用。

2020-11-30