2010-12-05 19 views
1

私は現在、データベースからテーブルにデータをフェッチできますが、リストビューを使用しています。私のコードは、すべてのデータを取得するためのコードです:データベースからのリストビューにデータを入力してください

public ArrayList<Object> getRowAsArray(long rowID) 
{ 
    // create an array list to store data from the database row. 
    // I would recommend creating a JavaBean compliant object 
    // to store this data instead. That way you can ensure 
    // data types are correct. 
    ArrayList<Object> rowArray = new ArrayList<Object>(); 
    Cursor cursor; 

    try 
    { 
     // this is a database call that creates a "cursor" object. 
     // the cursor object store the information collected from the 
     // database and is used to iterate through the data. 
     cursor = db.query 
     (
       TABLE_NAME, 
       new String[] { TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO, TABLE_ROW_THREE, TABLE_ROW_FOUR, TABLE_ROW_FIVE, TABLE_ROW_SIX, TABLE_ROW_SEVEN }, 
       TABLE_ROW_ID + "=" + rowID, 
       null, null, null, null, null 
     ); 

     // move the pointer to position zero in the cursor. 
     cursor.moveToFirst(); 

     // if there is data available after the cursor's pointer, add 
     // it to the ArrayList that will be returned by the method. 
     if (!cursor.isAfterLast()) 
     { 
      do 
      { 
       rowArray.add(cursor.getLong(0)); 
       rowArray.add(cursor.getString(1)); 
       rowArray.add(cursor.getString(2)); 
       rowArray.add(cursor.getString(3)); 
       rowArray.add(cursor.getString(4)); 
       rowArray.add(cursor.getString(5)); 
       rowArray.add(cursor.getString(6)); 
       rowArray.add(cursor.getString(7)); 
      } 
      while (cursor.moveToNext()); 
     } 

     // let java know that you are through with the cursor. 
     cursor.close(); 
    } 
    catch (SQLException e) 
    { 
     Log.e("DB ERROR", e.toString()); 
     e.printStackTrace(); 
    } 

    // return the ArrayList containing the given row from the database. 
    return rowArray; 
} 

どうすればリストビューに入れてください。何か助けていただければ幸いです

答えて

2

これはずっと簡単です。配列リストを作成する必要はありません。

「アダプタ」がキーです。アダプタは、リストとデータの間のインターフェイスです。あなたの場合、SimpleCursorAdapterが必要です。 Apiのデモにはその例があります。見てみな。

クエリのカーソルを渡すだけで、自動的にリストビューにデータが入力されます。

+0

チュートリアルは知っていますか?私は、私が必要としていたタイプには、しかし、誰もelooked。私はデータベースに7行を表示する – Fizzb89

+0

APIデモは非常に明確に構造化されています。 "Lists"のグループがあり、そこには "Cursorからのデータ"のグループがあります。右:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List7.html – EboMike

+0

おかげです。私はちょうど今見ている、彼らはそれをurisのために使用しているようだ。カーソルカーソル= managedQuery(getIntent()。getData()、PROJECTION、null、null、 NoteColumns.DEFAULT_SORT_ORDER); //データベースのノートエントリをビューにマップするときに使用します。 SimpleCursorAdapterアダプタ= new SimpleCursorAdapter(this、R.layout.noteslist_item、cursor、 new String [] {NoteColumns.TITLE}、new int [] {android.R .id.text1}); setListAdapter(adapter);どのように私にこのリンクします。おかげでね – Fizzb89

関連する問題