当前位置: 代码迷 >> Android >> 将来自 SQLite 的数据显示为 android 中的文本
  详细解决方案

将来自 SQLite 的数据显示为 android 中的文本

热度:102   发布时间:2023-08-04 11:59:15.0

我在 SQLite 中有一个表,其中包含一些数据(例如 NAME、AGE、GENDER)。我想将所有 NAME 显示为按钮中的文本,并且它应该能够根据项目的数量自动创建按钮(姓名)。

我想这样做是因为我想使用不同的 .xml 文件显示其余数据。 例如,名字是 JONE、KEN、PEN,所以当用户点击 JONE 时,它应该只显示他的姓名、年龄和性别。

请帮忙...

这会起作用

// Getting All Contacts
public ArrayList<Contact> Get_Contacts() {
try {
contact_list.clear();

// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhoneNumber(cursor.getString(2));
contact.setEmail(cursor.getString(3));
// Adding contact to list
contact_list.add(contact);
} while (cursor.moveToNext());
}

// return contact list
cursor.close();
db.close();
return contact_list;
} catch (Exception e) {
// TODO: handle exception
Log.e("all_contact", "" + e);
}

return contact_list;
}
  相关解决方案