开发者

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). All of the existing examples use the following:

   public void onCreate(Bundle savedInstanceState) {
     开发者_JAVA百科    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:

<?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:

<?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. Any help on creating the proper adapter would be appeciated.


Answered it with some help. Several things were wrong, mostly in the 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. You don't have a name for it, but you can use getListView() in the onCreate method to access it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜