0

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


ListAdapter 适配器 = new SimpleCursorAdapter(this,android.R.layout.two_line_list_item,cursor,from, to ); 或者可能


或者可能

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


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

4

1 回答 1

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

你自己的光标适配器类

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


}

列表视图的 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>
于 2012-11-27T09:26:05.710 回答