简体   繁体   English

尝试在带有光标但没有ListActivity的.xml中使用Listview

[英]Trying to use a listview in .xml with a cursor but without a ListActivity

I'm stumped as to how to properly implement an adapter that can pull a column of data from my sqlite database and add it to a listview (based on the android-provided multiple checkbox). 我很困惑如何正确实现一个适配器,该适配器可以从我的sqlite数据库中提取一列数据并将其添加到列表视图(基于android提供的多个复选框)。 All of the existing examples use the following: 所有现有示例均使用以下内容:

   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);
         }

However, I don't want a string. 但是,我不需要字符串。 Here's my code: 这是我的代码:

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);
    }

}

Here's the view.xml file: 这是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>

And, because the SimpleCursorAdapter asks for it, here's the grouprow.xml: 而且,由于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"/>

If I use a ListActivity and don't invoke the layout on Creation, I can get it to work, but I need the wrappers around that ListView, and I can't make the .addHeader and .addFooter methods work for me. 如果我使用ListActivity而不在Creation上调用布局,则可以使其正常工作,但是我需要该ListView的包装,并且我无法使.addHeader和.addFooter方法对我有用。 Any help on creating the proper adapter would be appeciated. 任何有关创建适当适配器的帮助将不胜感激。

Answered it with some help. 在一些帮助下回答了它。 Several things were wrong, mostly in the list.xml: 有几处错误,主要是在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>

This allows you to tie the ListView to that element in the xml. 这使您可以将ListView绑定到xml中的该元素。 You don't have a name for it, but you can use getListView() in the onCreate method to access it. 您没有名称,但是可以在onCreate方法中使用getListView()进行访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM