在我的数据库表中插入行并尝试获取它们以显示之后,我得到一个空白活动。它让我感到困惑,因为它曾经在一个月前工作但现在不再工作了。
我甚至经历过这个tutorial link来了解更多,但这一切都是徒劳的。
下面是数据库表的构造函数部分:
// Emptry constructor
public Contact() {
}
// Constructor
public Contact(int _contactid, String contactphone, String contactlmp) {
this._contactid = _contactid;
this.contactphone = contactphone;
this.contactlmp = contactlmp;
}
// Constructor
public Contact(String contactphone, String contactlmp) {
this.contactphone = contactphone;
this.contactlmp = contactlmp;
}
下面是我用来检索表中所有元素的数据库函数:
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<>();
String query = "SELECT * FROM " + TABLE_CONTACTS + ";";
SQLiteDatabase db = this.getWritableDatabase();
Cursor c = db.rawQuery(query, null);
if(c != null)
c.moveToFirst();
// Loop through all rows and add to list
while(!c.isAfterLast()) {
Contact contact = new Contact();
contact.set_contactid(Integer.parseInt(c.getString(0)));
contact.setContactphone(c.getString(1));
contact.setContactlmp(c.getString(2));
contactList.add(contact);
c.moveToNext();
}
return contactList;
}
以下是对此功能的调用,该功能使用结果在该活动上打印出来。它嵌入在activity的onCreate()函数中的一个线程中:
contacts = (TextView) findViewById(R.id.contacts);
dbHandler = new MyDBHandler(this, null, null, 1);
Runnable r = new Runnable() {
@Override
public void run() {
//dbString = dbHandler.databaseToString();
int i = 1;
dbContacts = dbHandler.getAllContacts();
for(Contact cn : dbContacts) {
dbString += i + " " + cn.getContactphone() + " " + cn.getContactlmp() + "\n";
i++;
}
}
};
Thread thread = new Thread(r);
thread.start();
contacts.setText(dbString);
以下是数据库联系人的getter和setter方法,这些方法在同一个应用程序的另一个上下文中使用时有效:
// Getter and Setter methods
public int get_contactid() {
return _contactid;
}
public void set_contactid(int _contactid) {
this._contactid = _contactid;
}
public String getContactlmp() {
return contactlmp;
}
public void setContactlmp(String contactlmp) {
this.contactlmp = contactlmp;
}
public String getContactphone() {
return contactphone;
}
public void setContactphone(String contactphone) {
this.contactphone = contactphone;
}
我可能做错了什么?它为什么突然停止工作?
答案 0 :(得分:0)
任何错误日志,或者你做了任何数据库更改(结构明智)
contacts = (TextView) findViewById(R.id.contacts);
dbHandler = new MyDBHandler(this, null, null, 1);
Runnable r = new Runnable() {
@Override
public void run() {
//dbString = dbHandler.databaseToString();
int i = 1;
dbContacts = dbHandler.getAllContacts();
for(Contact cn : dbContacts) {
dbString += i + " " + cn.getContactphone() + " " + cn.getContactlmp() + "\n";
i++;
}
contacts.setText(dbString);
}
};
Thread thread = new Thread(r);
thread.start();
答案 1 :(得分:0)
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
int i = 1;
dbContacts = dbHandler.getAllContacts();
for(Contact cn : dbContacts) {
dbString += i + " " + cn.getContactphone() + " " + cn.getContactlmp() + "\n";
i++;
}
contacts.setText(dbString);
}
};
Runnable r = new Runnable() {
@Override
public void run() {
handler.sendEmptyMessage(0);
}
};
Thread thread = new Thread(r);
thread.start();