我创建了一个可以将联系人从文本框保存到数据库的应用。联系人将是输入的号码或默认联系人。这个应用程序有大约5个编辑文本。
这里的问题是:第一次输入号码并单击“保存”按钮时,号码将被保存并正确检索。当我第二次为新号码做同样的事情。号码正在保存。当我尝试从DB中检索数字时,我看不到该数字。
我使用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;
}
}
我在这里找不到确切的问题。
答案 0 :(得分:0)
就我的理解而言。我认为问题在于getContacts方法:
根据您的逻辑,Contacts方法的一个对象将包含五个联系人,因此它将保存在数据库中。
但是你没有以正确的方式检索它们,所以必须创建一个类Contacts的数组,然后每个单独的行将包含五个联系人(这反映了列数)
所以做这样的事情:
// Getting contacts
Contacts[] getContacts()
{
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
int count = getContactsCount();
**//here can you check if the count is greater than 0**
Log.d("Database", "Count: " + count);
Contacts contacts[] = new Contacts[count];
int index = 0;
// looping through all rows and adding to list
if (cursor.moveToFirst())
{
do
{
Log.d("Database", "Initializing contacts...");
contacts[index] = new Contacts(); ///initialize contacts here
ArrayList<String> arr = new ArrayList<String>();
arr.add(cursor.getString(0));
arr.add(cursor.getString(1));
arr.add(cursor.getString(2));
arr.add(cursor.getString(3));
arr.add(cursor.getString(4));
contacts[index].setArrcontactsofbean(arr);
index++;
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return contacts;
}
您必须更改在代码中检索结果的方式,即处理数组然后显示。
“我尝试使用你说的arraylist方法检索值,它给了我一个空指针异常。” ?
尝试以下代码来遍历结果:
Contacts contactsFromDb[];
contactsFromDB = db.getContacts() /// Note the db is the object of Database class in which the getContacts() method is
if(contactsDB != null){
for(int k =0; k < contactsFromDB.length; k++){
arrforcontactsdb = contactsFromDB[k].getArrcontactsofbean();
for(int i = 0 ;i < (arrforcontactsdb.size());i++) //not it should not be -1
{
Log.e("retrieved VALUES ARE",""+arrforcontactsdb.get(i));
}
“你能让我知道你为什么在database.java中使用数组的arraylist。?”
<强>答案:强>
它不是Array的ArrayList ....它是包含每个单独联系人的String的ArrayList。它是数组的原因,因为每行将包含5个联系人表示一个Contacts对象,因此多行表示多个对象,因此我在retreiving时将它们存储在Array中。