Why is my custom layout file not recognized?

I created a file in \res\layout named contactlist.xml

But it is not recognized in my code:

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

I want to create a custom layout that for each Contact has three checkboxes.

Why is my custom layout not accepted as valid?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Updated 2/9/2012:

Finally!

With the help of stackOverflowers and this article: http://www.vogella.de/articles/AndroidListView/article.html

I finally got it working; as usual, it's not that tough once you grok a couple of concepts. It boils down to using this sort of code, which I begged/borrowed/stole and adapted:

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

}

...and making sure "ondemandandautomatic_authorize" (or whatever you name your layout file) is something like this (unfinished, but you get the idea):

<?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>

...and that "R.id.contactLabel" is replaced with "R.id."

There's more to be done, obviously, but a big obstacle has been hurdled.

Upvotes: 1

Views: 5430

Answers (5)

Omar
Omar

Reputation: 8145

Sometimes, even when you replace the android.R.layout.zzz with R.layout.zzz, Eclipse will still show you an error that your customer layout is no recognized.

I just faced this same problem now.

The solution for me was to remove that text and write it again. You may also write it gradually by writing "R." then pressing Alt+Space, Eclipse will give you 2 options (android.R and R.), just pick the R. and then continue the rest.

Upvotes: 1

AndoAiron
AndoAiron

Reputation: 694

use your application generated R file(R.layout.contactlist) rather than using android generated R file(android.R.layout.contactlist).

Upvotes: 3

Shashank Kadne
Shashank Kadne

Reputation: 8101

I think u missed setContentView(R.Layout.contactlist);...also u need to specify this in your manifest.

Upvotes: 1

April Smith
April Smith

Reputation: 1830

If you are using Eclipse, I think you should try re-start Eclipse. Sometimes, when i add new xml or delete some, my eclipse's not smart enough, i don't know why. There are other ways like Clean Project(But you have to make sure that there is No Error in your XML, otherwise your R will gone forever).

Not quite sure, hope it help

Upvotes: 1

blessanm86
blessanm86

Reputation: 31789

It should be

R.layout.contactlist

and not

android.R.layout.contactlist

android is used when you are using the system resources.

Upvotes: 8

Related Questions