根据其他autocompletetextview

时间:2015-06-27 10:55:50

标签: android sqlite

我编写了一个程序,可以根据单个autocompletetextview的选择自动在两个autocompletetextview中设置文本,但是会出错。

06-27 16:17:42.369:E / CursorWindow(7974):无法从CursorWindow中读取第0行,第1列,其中包含2行3列。

cursor = listHelper.getVehicleData();
                int[] vehicleNoTo = new int[] { android.R.id.text1 }; 
                String[] vehicleNoFrom = new String[] { 
                DbListHelper.REGISTRATION_NO };
                SimpleCursorAdapter driverAdapter = new 
                SimpleCursorAdapter(getBaseContext(),
                        android.R.layout.simple_list_item_1, cursor, 
                        vehicleNoFrom, vehicleNoTo);

                 driverAdapter.setCursorToStringConverter(new 
                 CursorToStringConverter() 
                 { 
                     public CharSequence convertToString(Cursor cur) 
                         { 
                         int index =  
                         cur.getColumnIndex(DbListHelper.REGISTRATION_NO); 
                         return cur.getString(index); 
                          } 
                     });

                 editVehicleNo1.setAdapter(driverAdapter);
                 editVehicleNo1.setThreshold(1);
                 driverAdapter.notifyDataSetChanged();

以上代码适用于一个autocompletetextview - 当我开始输入时显示车辆的注册号。

但我想要的是,当我选择任何车辆时,应该设置驱动程序名称(自动完成)和驱动程序代码(自动完成)。

所以我已经为上面的逻辑写了,但是在这里得到了一个错误:

editVehicleNo1.addTextChangedListener(new TextWatcher() {

                    @Override
                    public void onTextChanged(CharSequence s, int start, int 
                    before, int count) {


                    }

                    @Override
                    public void beforeTextChanged(CharSequence s, int start, 
                            int count,
                            int after) {


                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        driverName1 =cursor.getString(cursor.getColumnIndex
                                     (DbListHelper.DRIVER_NAME));
                        driverCode1 = cursor.getString(cursor.getColumnIndex
                                      (DbListHelper.DRIVER_CODE));
                        editDriverName.setText(driverName1);
                        editDriverCode.setText(driverCode1);

                    }
                });

用于插入和选择车辆数据库的SqliteHelper.java

public long insertVehicleData(String vehicle_code,String 
    registration_no,String driver_code,String driver_name) {
    SQLiteDatabase db = helper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(DbListHelper.VEHICLE_CODE, vehicle_code);
    values.put(DbListHelper.REGISTRATION_NO, registration_no);
    values.put(DbListHelper.DRIVER_CODE, driver_code);
    values.put(DbListHelper.DRIVER_NAME, driver_name);
    long id = db.insert(DbListHelper.TABLE_NAME_VEHICLE, null, values);
    return id;
}

public Cursor getVehicleData(){

    SQLiteDatabase db = helper.getWritableDatabase();
    String[] columns = { 
    DbListHelper.UID,DbListHelper.VEHICLE_CODE,DbListHelper.REGISTRATION_NO
            ,DbListHelper.DRIVER_CODE,DbListHelper.DRIVER_NAME};
    Cursor cursor = db.query(DbListHelper.TABLE_NAME_VEHICLE, columns, null, 
                    null, null, null, null);
    StringBuffer buffer = new StringBuffer();
    while (cursor.moveToNext()) {
        int index0 = cursor.getColumnIndex(DbListHelper.UID);
        String cid0 = cursor.getString(index0);
        int index1 = cursor.getColumnIndex(DbListHelper.VEHICLE_CODE);
        String cid1 = cursor.getString(index1);
        int index2 = cursor.getColumnIndex(DbListHelper.REGISTRATION_NO);
        String cid2 = cursor.getString(index2);
        int index3 = cursor.getColumnIndex(DbListHelper.DRIVER_CODE);
        String cid3 = cursor.getString(index3);
        int index4 = cursor.getColumnIndex(DbListHelper.DRIVER_NAME);
        String cid4 = cursor.getString(index4);

        buffer.append(cid0 + " " + cid1 + " " + cid2 + " " + cid3 + " " + 
        cid4 +  "\n");
    }
    return cursor;

} 

1 个答案:

答案 0 :(得分:0)

替换getVechileData()

中的SqliteHelper.java的while循环
 while (cursor.moveToNext()) {
        int index0 = cursor.getColumnIndex(DbListHelper.UID);
        String cid0 = cursor.getString(index0);
       //Your code.............
    }

 if (cursor.moveToFirst()) {
        do {
              int index0 = cursor.getColumnIndex(DbListHelper.UID);
              String cid0 = cursor.getString(index0);
              //Your code......
            } while (cursor.moveToNext());
  }
如果数据库中有任何记录,

cursor.moveToFirst()必须在查询后调用,否则返回false。

onTextChanged()中,您无法直接从光标中获取值,请先调用cursor.moveToFirst(),然后获取值。

 @Override
 public void afterTextChanged(Editable s) {
      if (cursor.moveToFirst(
             driverName1 =cursor.getString(cursor.getColumnIndex (DbListHelper.DRIVER_NAME));
          }
     }