I have written a program to set text automatically in two autocompletetextview based on selection of single autocompletetextview but getting an error.
06-27 16:17:42.369: E/CursorWindow(7974): Failed to read row 0, column -1 from a CursorWindow which has 2 rows, 3 columns.
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();
The above code works perfectly for one autocompletetextview showing registration nos of vehicle when i start typing.
But what i want is , when i select on any vehicle nos , driver name(autocomplete) and driver code(autocomplete) should get set.
So i have written for the above logic , but gets an error here
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 for insertion and selection of vehicle database
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;
}
Aucun commentaire:
Enregistrer un commentaire