scarhand
scarhand

Reputation: 4337

Populating a dynamically created ListView with an ArrayList

I'm trying to populate a dynamically created ListView with an ArrayList that is fetched from another function. I'm getting the error "The constructor ArrayAdapter<String>(ShowRecords, ListView, ArrayList<String>) is undefined". Here's my code for the ListActivity:

public class ShowRecords extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);

        DatabaseHandler db = new DatabaseHandler(this);
        ArrayList<String> records = db.getRecords();

        ListView lv = new ListView(this);
        this.setListAdapter(new ArrayAdapter<String>(this, lv, records));
    } 

}

Here's my code for the getRecords() function:

public ArrayList<String> getRecords() {
    ArrayList<String> recordList = new ArrayList<String>();
    String selectQuery = "SELECT millis FROM records ORDER BY CAST(millis as SIGNED) DESC LIMIT 10";

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

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {
                recordList.add(cursor.getString(0));
            } while (cursor.moveToNext());
        }
    }

    return recordList;
}

How do I fix this?

Upvotes: 0

Views: 1075

Answers (2)

Rymnel
Rymnel

Reputation: 4515

Since you're using a ListActivity you don't need to declare the listview.

Try this, this should work!

public class ShowRecords extends ListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);

    DatabaseHandler db = new DatabaseHandler(this);
    ArrayList<String> records = db.getRecords();
    setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, records));
} 

}

Upvotes: 1

Gomoku7
Gomoku7

Reputation: 1372

Here's the list of available constructors :

[Public Method] [Constructor] ArrayAdapter(Context, int) : void
[Public Method] [Constructor] ArrayAdapter(Context, int, int) : void
[Public Method] [Constructor] ArrayAdapter(Context, int, Object[]) : void
[Public Method] [Constructor] ArrayAdapter(Context, int, List) : void
[Public Method] [Constructor] ArrayAdapter(Context, int, int, List) : void
[Public Method] [Constructor] ArrayAdapter(Context, int, int, Object[]) : void

You certainly want to use this one :

[Public Method] [Constructor] ArrayAdapter(Context, int, Object[]) : void

So it means :

this.setListAdapter(new ArrayAdapter<String>(this, R.id.whateveridyouchoose, records));

Upvotes: 0

Related Questions