simpleCursorAdapter没有ListActivity?

时间:2012-11-27 09:19:45

标签: android database eclipse sqlite simplecursoradapter

请您给我一些如何在没有ListActivity的情况下使用simpleCursorAdapter的提示? 我的意思是,我想开发一个使用simpleCursorAdapter而没有listActivity的应用程序, 坦率地说,如果我不使用ListActivity,我应该如何设置simpleCursorAdapter项目, 例如


ListAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.two_line_list_item,cursor,from, to );或者


或者

ListAdapter adapter = new SimpleCursorAdapter(this,.R.layout.mypage,cursor,from, to );


我的问题是“”项目,因为我在这个页面中没有任何TextView(XML文件),我想要在listView中显示的树字段,我在my中定义XML文件为“mylist”,

1 个答案:

答案 0 :(得分:0)

在您的活动中

private String [] project = {ContactsContract.CommonDataKinds.Phone._ID,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
            ContactsContract.CommonDataKinds.Phone.NUMBER};

...

    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                        project, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    startManagingCursor(phones);
    ContactCursorAdapter adapter = new ContactCursorAdapter(this, phones);
    ListView contactLV = (ListView) findViewById(R.id.contactLV);
    contactLV.setAdapter(adapter);
...

您自己的Cursor Adapter类

 public class ContactCursorAdapter extends CursorAdapter {

        public ContactCursorAdapter(Context context, Cursor c) {
            super(context, c);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            TextView nameTV = (TextView)view.findViewById(R.id.nameTV);
            nameTV.setText(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
            TextView phoneTV = (TextView)view.findViewById(R.id.phoneTV);
            phoneTV.setText(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            LayoutInflater inflater = LayoutInflater.from(context);
            View v = inflater.inflate(R.layout.contact_for_lv, parent, false);
            bindView(v, context, cursor);
            return v;
        }


}

ListView的XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <TextView
        android:id="@+id/nameTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/phoneTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:textSize="14sp" />

</LinearLayout>