2012-02-07 83 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.然後繼續。

相關問題