0
我創建了一個包含我的SQL Db的一般結構的Contact類。我想收到的列表,所以我用下面的代碼在我的MainActivity在ListView中使用List <Custom_Class>
List<Contact> contacts = db.getAllContacts();
// Error in this line
ArrayAdapter adapter = new ArrayAdapter<List<Contact>>(this, R.layout.activity_main, contacts);
contactList.setAdapter(adapter);
這給了錯誤Cannot resolve constructor
的getAllContacts()的形式,我的數據是這樣的
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;
}
那麼如何以列表形式接收我的數據並填充我的列表?
使用'新ArrayAdapter'而不是'新ArrayAdapter <列表>' –
Blackbelt
呼叫'ArrayAdapter'這樣的構造函數: 'ArrayAdapter adapter = new ArrayAdapter(this,R.layout.activity_main,contacts);' https://developer.android.com/reference/android/widget/ArrayAdapter.html# ArrayAdapter(android.content.Context,int,java.util.List) –