我创建了一个可以将联系人从文本框保存到数据库的应用程序。联系人将是输入的号码或默认联系人。这个应用程序有大约 5 个编辑文本。
这里的问题是:当我第一次输入号码并单击保存按钮时,号码被保存并且也被正确检索。当我第二次为新号码做同样的事情时。号码正在保存中。当我尝试从数据库中检索数字时,我看不到该数字。
我正在使用 getter 和 setter 方法来存储事务值。
以下是我尝试过并遇到问题的方法:
通过 getter 和 setter 方法从 edittext 到数据库。
Contacts contacts = new Contacts();
//arrsendcontactsinverse is the arraylist which stores all the numbers
contacts.setArrcontactsofbean(arrsendcontactsinverse);
database.addContacts(contacts);
//检索我使用下面的代码的数据。
db = new DataBase(this);
if(db.getContactsCount()>0)
{
Contacts contactFromDb = db.getContacts();
contact1.setText(contactFromDb.getArrcontactsofbean().get(0));
contact2.setText(contactFromDb.getArrcontactsofbean().get(1));
contact3.setText(contactFromDb.getArrcontactsofbean().get(2));
contact4.setText(contactFromDb.getArrcontactsofbean().get(3));
contact5.setText(contactFromDb.getArrcontactsofbean().get(4));
}
}
这是DataBase.java 中的代码。
public class DataBase extends SQLiteOpenHelper
{
private static final int DATABASE_VERSION = 1;
private static final String TABLE_CONTACTS = "ContactsTable";
private static final String KEY_CONTACT1 = "contact1"; //column name
private static final String KEY_CONTACT2 = "contact2"; //column name
private static final String KEY_CONTACT3 = "contact3"; //column name
private static final String KEY_CONTACT4 = "contact4"; //column name
private static final String KEY_CONTACT5 = "contact5"; //column name
ArrayList<String> arrcontactofdatabasehandler= new ArrayList<String>();
public Databasehandler(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db)
{
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS +"("
+KEY_CONTACT1+ " TEXT,"
+KEY_CONTACT2+ " TEXT,"
+KEY_CONTACT3+ " TEXT,"
+KEY_CONTACT4+ " TEXT,"
+KEY_CONTACT5+ " TEXT,"
+ ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Adding new contact
void addContacts(Contacts contacts)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_CONTACT1,contacts.getArrcontactsofbean().get(0));
values.put(KEY_CONTACT2,contacts.getArrcontactsofbean().get(1));
values.put(KEY_CONTACT3,contacts.getArrcontactsofbean().get(2));
values.put(KEY_CONTACT4,contacts.getArrcontactsofbean().get(3));
values.put(KEY_CONTACT5,contacts.getArrcontactsofbean().get(4));
db.insert(TABLE_CONTACTS, null, values);
db.close();
}
// Getting contacts
Contacts getContacts()
{
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
Contacts contacts = new Contacts();
// looping through all rows and adding to list
if (cursor.moveToFirst())
{
do
{
arrcontactofdatabasehandler.add(cursor.getString(0));
arrcontactofdatabasehandler.add(cursor.getString(1));
arrcontactofdatabasehandler.add(cursor.getString(2));
arrcontactofdatabasehandler.add(cursor.getString(3));
arrcontactofdatabasehandler.add(cursor.getString(4));
} while (cursor.moveToNext());
}
contacts.setArrcontactsofbean(arrcontactofdatabasehandler);
cursor.close();
db.close();
return contacts;
}
public int getContactsCount()
{
String countQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int count = cursor.getCount();
cursor.close();
// return count
db.close();
return count;
}
public void dropTable()
{
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS +"("
+KEY_CONTACT1+ " TEXT,"
+KEY_CONTACT2+ " TEXT,"
+KEY_CONTACT3+ " TEXT,"
+KEY_CONTACT4+ " TEXT,"
+KEY_CONTACT5+ " TEXT,"
+ ")";
db.execSQL(CREATE_CONTACTS_TABLE);
db.close();
}
}
Contacts.java.(这些是 getter 和 setter 方法)
public class Contacts
{
ArrayList<String> arrcontactsofbean= new ArrayList<String>();
public Contacts()
{
}
public ArrayList<String> getArrcontactsofbean()
{
return arrcontactsofbean;
}
public void setArrcontactsofbean(ArrayList<String> arrcontactsofbean)
{
this.arrcontactsofbean = arrcontactsofbean;
}
}
我在这里找不到确切的问题。