2012-02-07 87 views
1

我在\资源创建的文件\布局名为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 }); 

我想创建一个自定义布局,对于每个联系人有三个复选框。

为什么我的自定义布局不被接受为有效?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 更新2012年2月9日:

终于来了!

随着stackOverflowers的帮助和这篇文章:http://www.vogella.de/articles/AndroidListView/article.html

我终于得到了它的工作;像往常一样,一旦你理解了一些概念,这并不难。它归结为使用这种代码,这是我恳求/借入/偷走,并适于:

@Override 公共无效的onCreate(捆绑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); 

}

...,并确保 “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> 

......并将“R.id.contactLabel”替换为“R.id.”

显然,还有很多事情要做,但是一个巨大的障碍已经被阻止。

回答

7

应该

R.layout.contactlist 

,而不是

android.R.layout.contactlist 

Android是当你正在使用的系统资源的使用。

1

如果您使用Eclipse,我认为您应该尝试重新启动Eclipse。有时,当我添加新的XML或删除一些,我的月食不够聪明,我不知道为什么。还有其他一些方法,像Clean Project(但是你必须确保你的XML没有错误,否则你的R将永远消失)。

不太清楚,希望这有助于

1

我觉得你错过的setContentView(R.Layout.contactlist); ...也ü需要在清单注明。

+0

指定清单中的内容? – 2012-02-07 16:38:06

2

使用您的应用程序生成的R文件(R.layout.contactlist)而不是使用android生成的R文件(android.R.layout.contactlist)。

1

有时候,即使您用R.layout.zzz替换android.R.layout.zzz,Eclipse仍然会向您显示错误,表明您的客户布局未被识别。

我现在只是面临同样的问题。

我的解决方案是删除该文本并重新写入。 你也可以通过写“R”来逐渐写出它。然后按Alt + Space,Eclipse会给你2个选项(android.R和R.),只需选择R.然后继续。

相关问题