2012-05-28 67 views
0

我試圖填充動態創建的ListView與從另一個函數獲取的ArrayList。我收到錯誤"The constructor ArrayAdapter<String>(ShowRecords, ListView, ArrayList<String>) is undefined"。這裏是我的ListActivity代碼:使用ArrayList填充動態創建的ListView

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

我該如何解決這個問題?

+2

爲什麼從數據庫創建一個ArrayList,而不僅僅是使用simplecursoradapter? –

+0

,因爲這是第一次以前曾經嘗試過這一點,我不完全確定我在做什麼 – scarhand

+0

您可以使用[本頁]第12行下面的部分(http://thinkandroid.wordpress.com/2010/01/ 09/simplecursoradapters和 - 列表視圖/)。 –

回答

1

由於您使用的是ListActivity你不需要聲明listview。

試試這個,這應該工作!

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

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