2011-03-18 45 views
0

我难以理解如何正确实现可以从我的sqlite数据库中提取一列数据并将其添加到列表视图的适配器(基于android提供的多个复选框)。所有现有示例使用以下内容:尝试在.xml中使用带有游标但没有ListActivity的列表视图

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     String[] myList = new String[] {"Hello","World","Foo","Bar"};    
     ListView lv = new ListView(this); 
     lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myList)); 
     setContentView(lv); 
     } 

但是,我不想要字符串。这里是我的代码:

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()来访问它。

相关问题