2011-03-18 12 views
0

sqliteデータベースからデータの列を引き出し、それをリストビューに追加できるアダプタを正しく実装する方法がわかりませんアンドロイド提供の複数チェックボックス)。既存の例はすべて次のものを使用しています:カーソルを使用してListActivityを使用せずに.xmlのリストビューを使用しようとしています

ただし、私は文字列を必要としません。ここで

public class ListGroups extends Activity { 
    /** Called when the activity is first created. */ 
    private ExpensesDbAdapter mDBHelper; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.view); 
     ListView lv = (ListView)findViewById(R.id.list1); 
     mDBHelper = new ExpensesDbAdapter(ListGroups.this); 
     mDBHelper.open(); 
     Cursor c = mDBHelper.fetchAllGroups(); 
     startManagingCursor(c); 
     String[] from = new String[] {ExpensesDbAdapter.KEY_GROUPNAME}; 
     int[] to = new int[] {R.id.groupname}; 
     ListAdapter ladapter = new SimpleCursorAdapter(ListGroups.this, R.layout.grouprow, c, from, to); 
     lv.setAdapter(ladapter); 
     lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
    } 

} 

view.xmlファイルです::ここに私のコードは

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center"> 
    <TextView android:id="@+id/header01" 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:gravity="center" 
       android:text="Select Groups"/> 
    <ListView 
     android:id="@+id/list1" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/footerbutton01" 
     android:text="Get Expense Reports" /> 
</LinearLayout> 

そして、SimpleCursorAdapterがそれを要求するので、ここではgrouprow.xmlです:

<?xml version="1.0" encoding="utf-8"?> 
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/groupname" 
    android:layout_width="match_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:gravity="center_vertical" 
    android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
    android:paddingLeft="6dip" 
    android:paddingRight="6dip"/> 

私が使用している場合ListActivityを作成し、Creationのレイアウトを呼び出さないと、動作させることができますが、そのListViewの周りにラッパーが必要です.addHeaderメソッドと.addFooterメソッドは私のために機能しません。適切なアダプターを作成する上での助けとなります。

答えて

0

何か助けを借りて回答しました。主にlist.xmlにいくつか間違っていました:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center"> 
    <TextView android:id="@+id/header01" 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:gravity="center" 
       android:text="Select Groups"/> 
    <ListView 
     android:id="@android:id/android:list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_around" 
     android:layout_weight="1" /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/footerbutton01" 
     android:text="Get Expense Reports" /> 
</LinearLayout> 

これにより、ListViewをxmlの要素に結びつけることができます。名前はありませんが、onCreateメソッドのgetListView()を使用してアクセスできます。

関連する問題