我有以下代码用于在我的应用程序中显示联系人。我在显示联系人时遇到问题。我的数组(来自)仅从数据库中获取一个值,但在数据库中存在许多值。如何从数据库中获取所有值。
我的数据库代码:
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// 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));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
GetvalueFromDb 代码:
List<Contact> contacts = db.getAllContacts();
for (Contact cn : contacts) {
from = new String[] { cn.getName()};
Toast.makeText(getApplicationContext(), ""+Arrays.toString(from), Toast.LENGTH_LONG).show();
}
note = new ArrayAdapter<String>(getApplicationContext(), R.layout.contact_edit,from);
lv.setAdapter(note);