2011-07-01 15 views
0

私は製品でリストビューを作成しようとしています。私はそれを行うことができますが、私はそれらのカスタムID(SQLiteデータベースからのもの)を持っている必要があります。これどうやってするの?そして「列」はマッピングを定義する「」ListViewのカスタムID

String[] columns = new String[] { "_id", "name" }; 
    int[] to = new int[] { android.R.id.text1, android.R.id.text2 }; 

    SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, columns, to); 
    lv.setAdapter(mAdapter)); 

:あなたはSimpleCursorAdapterを使用する必要があります

ArrayList<String> productNameList = new ArrayList<String>(); 
    ArrayList<String> productIdList = new ArrayList<String>(); 
    Cursor results = myDbHelper.getProducts(); 
    ListView lv = (ListView) findViewById(R.id.productlist); 

    while (results.moveToNext()){ 
     productNameList.add(results.getString(results.getColumnIndex("name"))); 
     productIdList.add(results.getString(results.getColumnIndex("id"))); 
    } 


    lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , productNameList)); 

答えて

0

:これは、これまでのid実装せずに私のコードです 「columsは」のリストでありますSimpleCursorAdapterで読み取るデータベースの列名。そして "to"は、どのウィジェットでカーソルからの値が設定されているかを指定するアンドロイドIDのリストです。

これは、アンドロイドIDが "android.R.id.text1"のテキストフィールドに "_id"の値が表示され、カラム "name"の値がテキストフィールド " android.R.txt2 "

+0

"to"は何を行い、my_cell_layoutは何ですか?ありがとう。 –

+0

コードを編集しました – thaussma

関連する問題