0

我正在尝试使用从另一个函数获取的动态创建ListView的填充。ArrayList我得到了错误"The constructor ArrayAdapter<String>(ShowRecords, ListView, ArrayList<String>) is undefined"。这是我的代码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));
    } 

}

这是我的getRecords()函数代码:

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

我该如何解决?

4

2 回答 2

1

由于您使用的是 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();
    setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, records));
} 

}
于 2012-05-28T14:26:21.060 回答
0

这是可用构造函数的列表:

[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

你当然想使用这个:

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

所以这意味着:

this.setListAdapter(new ArrayAdapter<String>(this, R.id.whateveridyouchoose, records));
于 2012-05-28T14:26:10.927 回答