0
這是我的SQLite表 ListView控件使用SimpleCursorAdapter獲取ID錯誤
我想用onItemClick
獲得ID。 這是我的代碼:
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub
int id_To_Search = (int)arg3;
//Toast.makeText(getActivity(), arg0.getItemAtPosition(arg2).getLong(0), Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), "ID: " + arg3 , Toast.LENGTH_LONG).show();
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(getActivity().getApplicationContext(),DisplayContact1.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
但onItemClick
正在通過數據庫行「名稱」。 幫我嗎?
這是我的DBHelper
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "MyDBName.db";
public static final String CONTACTS_TABLE_NAME = "contacts";
public static final String CONTACTS_COLUMN_NAME = "name";
public static final String CONTACTS_COLUMN_EMAIL = "email";
public static final String CONTACTS_COLUMN_STREET = "street";
public static final String CONTACTS_COLUMN_CITY = "place";
public static final String CONTACTS_COLUMN_PHONE = "phone";
public static final String CONTACTS_COLUMN_INCOME = "income";
public static final String CONTACTS_COLUMN_LEGEND = "legend";
public static final String CONTACTS_COLUMN_DATE = "date";
private HashMap hp;
SQLiteDatabase db = null;
public DBHelper(Context context) {
super(context, DATABASE_NAME , null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table contacts " + "(_id integer primary key, name text,phone text,email text, street text,place text)"
);
db.execSQL("create table contacts2 " + "(id integer primary key,income text, legend text, date text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS contacts");
db.execSQL("DROP TABLE IF EXISTS contacts2");
onCreate(db);
}
public Cursor select()
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query("contacts", null, null, null, null, null, null);
return cursor;
}
public Cursor select2()
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query("contacts2", null, null, null, null, null, null);
return cursor;
}
public boolean insertContact (String name, String phone, String email, String street,String place) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("phone", phone);
contentValues.put("email", email);
contentValues.put("street", street);
contentValues.put("place", place);
db.insert("contacts", null, contentValues);
return true;
}
public boolean insertContact2 (String income, String legend, String date) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("income", income);
contentValues.put("legend", legend);
contentValues.put("date", date);
db.insert("contacts2", null, contentValues);
return true;
}
public Cursor getData(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from contacts where id="+id+"", null);
return res;
}
public Cursor getData2(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from contacts2 where id="+id+"", null);
return res;
}
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, CONTACTS_TABLE_NAME);
return numRows;
}
public boolean updateContact (Integer id, String name, String phone, String email, String street,String place) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("phone", phone);
contentValues.put("email", email);
contentValues.put("street", street);
contentValues.put("place", place);
db.update("contacts", contentValues, "id = ? ", new String[] { Integer.toString(id) });
return true;
}
public boolean updateContact2 (Integer id, String income, String legend, String date) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("income", income);
contentValues.put("legend", legend);
contentValues.put("date", date);
db.update("contacts2", contentValues, "id = ? ", new String[] { Integer.toString(id) });
return true;
}
public Integer deleteContact (Integer id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("contacts", "id = ? ", new String[] { Integer.toString(id) });
}
public Integer deleteContact2 (Integer id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("contacts2", "id = ? ", new String[] { Integer.toString(id) });
}
public ArrayList<String> getAllCotacts() {
ArrayList<String> array_list = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from contacts", null);
res.moveToFirst();
while(res.isAfterLast() == false){
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME)));
res.moveToNext();
}
return array_list;
}
public ArrayList<Float> getFloat() {
ArrayList<Float> array_list = new ArrayList<Float>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from contacts", null);
res.moveToFirst();
while(res.isAfterLast() == false){
array_list.add(res.getFloat(res.getColumnIndex(CONTACTS_COLUMN_NAME)));
res.moveToNext();
}
return array_list;
}
public ArrayList<String> getItem() {
ArrayList<String> array_list = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from contacts", null);
res.moveToFirst();
while(res.isAfterLast() == false){
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_PHONE)));
res.moveToNext();
}
return array_list;
}
public ArrayList<String> getAllCotacts2() {
ArrayList<String> array_list = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from contacts2", null);
res.moveToFirst();
while(res.isAfterLast() == false){
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_INCOME)));
res.moveToNext();
}
return array_list;
}
}
我該怎麼辦獲得ID?
ps。我用cursor.getString(cursor.getColumnIndex("id")
但錯誤是: Couldn't read row 2, col -1 from CursorWindow.