我在此处遵循此API指南:http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews
这是以上链接的代码段:
String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
int[] toViews = {R.id.display_name, R.id.phone_number};
我的问题是..在布局上是什么是R.id.display_name和R.id.phone_number?如何在布局.xml中定义它?这与listView容器有什么关系?
另外,R.layout.person_name_and_number如何定义如下所示?
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.person_name_and_number, cursor, fromColumns, toViews, 0);
ListView listView = getListView();
listView.setAdapter(adapter);
感谢。
答案 0 :(得分:1)
根据记录的构造函数源代码
SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from,
int[] to, int flags)
/**
* Standard constructor.
*
* @param context The context where the ListView associated with this
* SimpleListItemFactory is running
* @param layout resource identifier of a layout file that defines the views
* for this list item. The layout file should include at least
* those named views defined in "toViews"
* @param c The database cursor. Can be null if the cursor is not available yet.
* @param from A list of column names representing the data to bind to the UI. Can be null
* if the cursor is not available yet.
* @param toViews that should display column in the "from" parameter.
* These should all be TextViews. The first N views in this list
* are given the values of the first N columns in the from
* parameter. Can be null if the cursor is not available yet.
* @param flags Flags used to determine the behavior of the adapter,
* as per {@link CursorAdapter#CursorAdapter(Context, Cursor, int)}.
*/
因此,您的布局person_name_and_number.xml
应包含2个TextViews
.. ID为R.id.display_name, R.id.phone_number
,如下所示
<?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="wrap_content" >
<TextView
android:id="@+id/display_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28dip" />
<TextView
android:id="@+id/phone_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28dip" />
</LinearLayout>
您的列表布局my_layout.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="wrap_content" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
因此,您的活动可以定义如下
public class MyListActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstance) {
setContentView(R.layout.my_layout);
String[] columnsForCursor = new String[] {
ContactsContract.Data._id, ContactsContract.Data.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER };
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,columnsForCursor, null, null, null);
int[] toViews = new int[] { R.id.display_name, R.id.phone_number };
String[] columnsForView = new String[]{ ContactsContract.Data.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER
};
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.list_example_entry, cursor, columnsForView, toViews);
ListView listView = getListView();
setListAdapter(mAdapter);
}
}
没有必要从ListActivity
扩展您的活动。您可以按如下方式初始化布局中定义的ListView对象
public class MyActivity extends Activity {
ListView mListView;
SimpleCursorAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstance) {
setContentView(R.layout.my_layout);
String[] columnsForCursor = new String[] {
ContactsContract.Data._id, ContactsContract.Data.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER };
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, columnsForCursor, null, null, null);
int[] toViews = new int[] { R.id.display_name, R.id.phone_number };
String[] columnsForView = new String[]{ ContactsContract.Data.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER };
mAdapter = new SimpleCursorAdapter(this, R.layout.list_example_entry, cursor, columns, toViews);
mListView = (ListView) findViewById(R.id.my_listView);
mListView.setListAdapter(mAdapter);
} // On Create
} // MyActivity
更新:为了重现上述代码示例,您的应用必须具有READ_CONTACTS
权限。要请求此操作,请将此元素作为<manifest>
<uses-permission android:name="android.permission.READ_CONTACTS" />