0

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?

3
  • 2
    Why are you creating a ArrayList from your database and not just using a simplecursoradapter? Commented May 28, 2012 at 14:18
  • because this is the first time ive ever tried this and am not totally sure what i am doing Commented May 28, 2012 at 14:20
  • You could just use the part below line 12 of this page. Commented May 28, 2012 at 14:28

2 Answers 2

1

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

}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.