B. *_*non 1 android android-layout android-listview android-xml
我在\ res\layout中创建了一个名为contactlist.xml的文件
但我的代码中无法识别它:
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
//android.R.layout.simple_list_item_1, mContacts, //if cre8 own layout, replace "simple_[etc]"
//android.R.layout.simple_list_item_checked, mContacts, // or simple_list_item_multiple_choice
//android.R.layout.simple_list_item_multiple_choice, mContacts,
android.R.layout.contactlist, mContacts, // <- contact list ist xml-non-grata
new String[] { ContactsContract.Contacts.DISPLAY_NAME },
new int[] { android.R.id.text1 });
Run Code Online (Sandbox Code Playgroud)
我想创建一个自定义布局,每个联系人都有三个复选框.
为什么我的自定义布局不被接受为有效?
2012年2月9日更新了~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
最后!
借助stackOverflowers和本文:http://www.vogella.de/articles/AndroidListView/article.html
我终于搞定了; 像往常一样,一旦你理解了几个概念,它就不那么难了.归结为使用这种代码,我乞求/借用/窃取和改编:
@Override public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);
// Return all contacts, ordered by name
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME };
mContacts = managedQuery(ContactsContract.Contacts.CONTENT_URI,
projection, null, null, ContactsContract.Contacts.DISPLAY_NAME);
// Display all contacts in a ListView
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
R.layout.ondemandandautomatic_authorize, mContacts,
new String[] { ContactsContract.Contacts.DISPLAY_NAME },
new int[] { R.id.contactLabel });
setListAdapter(mAdapter);
Run Code Online (Sandbox Code Playgroud)
}
...并确保"ondemandandautomatic_authorize"(或任何你命名的布局文件)是这样的(未完成,但你明白了):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
<TextView
android:id="@+id/contactLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="(replace this)"
android:textSize="20px" >
</TextView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
......并且"R.id.contactLabel"被替换为"R.id."
显然,还有更多工作要做,但是遇到了一个很大的障碍.
它应该是
R.layout.contactlist
Run Code Online (Sandbox Code Playgroud)
并不是
android.R.layout.contactlist
Run Code Online (Sandbox Code Playgroud)
在使用系统资源时使用android.
| 归档时间: |
|
| 查看次数: |
4847 次 |
| 最近记录: |