我需要有关我的 android 简单应用程序的帮助。我尝试了很多来解决它,但它没有用。当我使用该additem()
方法时,我遇到了一个异常:
android.database.cursorindexoutofboundsexception index 0 requested with a size of 0
数据库代码
public class DBAdapter extends SQLiteOpenHelper
{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "Items_Manger";
private static final String Table = "Items";
private static final String creat_Table=
"CREATE TABLE if not exists Items(id integer primary key autoincrement,name text,price INTEGER)";
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_price = "price";
public DBAdapter(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(creat_Table);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + Table);
onCreate(db);
}
void additem(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID, contact.get_id());
values.put(KEY_NAME, contact.get_name());
values.put(KEY_price, contact.get_price());
db.insert(Table, null, values);
db.close();
}
Contact getitem(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(Table, new String[] { KEY_ID,
KEY_NAME, KEY_price }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)), cursor.getString(1),
Integer.parseInt(cursor.getString(2)));
// return contact
return contact;
}
public List<Contact> getallitems() {
List<Contact> contactList = new ArrayList<Contact>();
String selectQuery = "SELECT * FROM " + Table;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.set_id(Integer.parseInt(cursor.getString(0)));
contact.set_name(cursor.getString(1));
contact.set_price(Integer.parseInt(cursor.getString(2)));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
return contactList;
}
//**************************************************************
public int updateitem(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.get_name());
values.put(KEY_price, contact.get_price());
return db.update(Table, values, KEY_ID + " = ?",
new String[] { String.valueOf(contact.get_id()) });
}
//**************************************************************
public void deleteitem(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(Table, KEY_ID + " = ",new String[] { String.valueOf(contact.get_id()) });
db.close();
}
按钮 :
insert.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int idd= Integer.parseInt(eid.getText().toString());
String namee= ename.getText().toString();
int pricee = Integer.parseInt(eprice.getText().toString());
Contact c = new Contact();
c=db.getitem(idd);
db.additem(new Contact(idd,namee,pricee));
}
});
联系人类
public class Contact {
int id;
int price;
String name;
public Contact(){}
public Contact(int _id, String _name, int _price){
this.id = _id;
this.name = _name;
this.price=_price;
}
public Contact(String _name, int _price){
this.name = _name;
this.price=_price;
}
public Contact(int _id){
this.id=_id;
}
public int get_id(){return this.id;}
public int get_price(){return this.price;}
public String get_name(){return this.name;}
public void set_id(int _id){this.id=_id;}
public void set_price(int _price){this.price=_price;}
public void set_name(String _name){this.name=_name;}
}