【问题标题】:Trying to use a listview in .xml with a cursor but without a ListActivity尝试在 .xml 中使用带有光标但没有 ListActivity 的列表视图
【发布时间】:2011-03-18 01:01:48
【问题描述】:

我不知道如何正确实现一个适配器,该适配器可以从我的 sqlite 数据库中提取一列数据并将其添加到列表视图(基于 android 提供的多个复选框)。所有现有示例都使用以下内容:

   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 方法为我工作.任何有关创建正确适配器的帮助都会受到欢迎。

【问题讨论】:

    标签: android listview cursor adapter


    【解决方案1】:

    在一些帮助下回答了这个问题。有几处出错,主要在 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() 来访问它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-09
      • 1970-01-01
      • 2020-10-19
      • 1970-01-01
      • 1970-01-01
      • 2012-05-25
      • 1970-01-01
      • 2019-05-19
      相关资源
      最近更新 更多