JCDenton
JCDenton

Reputation: 151

Using List<Custom_Class> in ListView

Ive created a class Contact which contains the general structure of my SQL Db. I wish to recieve my data in the form of a List so I use the following code in my MainActivity

List<Contact> contacts = db.getAllContacts();
// Error in this line
ArrayAdapter adapter = new ArrayAdapter<List<Contact>>(this, R.layout.activity_main, contacts);

contactList.setAdapter(adapter);

This gives the error Cannot resolve constructor

the getAllContacts() looks like this

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;
}

So how do I receive my data as List and populate my list?

Upvotes: 0

Views: 40

Answers (1)

Hardik Senghani
Hardik Senghani

Reputation: 64

Template of the ArrayAdapter is the record type, in your case Customer, not List so correct code will be

ArrayAdapter adapter = new ArrayAdapter<Contact>(this, R.layout.activity_main, contacts);

Upvotes: 1

Related Questions