0

I've a very basic question here: I'm creating an android list using an ArrayAdapter which populates data to a listView from a string array:

final String[] listData={"Red", "Green", "Blue",
                "Purple", "Orange"};
        final ListView lv=(ListView)findViewById(R.id.listView1);

        final ArrayAdapter<String> aa=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listData);
        lv.setAdapter(aa);

The above code works fine. My question is, why can't we use the following adapter implementation:

final ArrayAdapter<String> aa=new ArrayAdapter<String>(this, R.id.listView1, listData);

listView1 is the id of my ListView in activity_main.xml. I tried using this implementation but the application crashed. What am I missing here? Sorry if the question seems silly as I'm still learning how to program android! Thanks for your help!

4 Answers 4

2

You have

ArrayAdapter<String> aa=new ArrayAdapter<String>(this, R.id.listView1, listData);

Wrong params for the ArrayAdapter Constructor

Look at the public onstructors @

http://developer.android.com/reference/android/widget/ArrayAdapter.html

ArrayAdapter(Context context, int resource, T[] objects)

The first param is a context

The second param is a resource

The third param is array of objects

So going by the above your code ArrayAdapter<String> aa=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listData);

Here you are using a in built layout form the android frame work

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.3_r1/frameworks/base/core/res/res/layout/simple_list_item_1.xml?av=f

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
    android:paddingRight="?android:attr/listPreferredItemPaddingRight"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>

ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)

The second param is a resource which should be a layout with text view

The third param is a textview id

Sign up to request clarification or add additional context in comments.

Comments

1

listView1 is the id of my ListView in activity_main.xml.

the constructor is expecting the id of a TextView where to put the text you provided through listData

Comments

1

You're tring to use a custom layout with a not-custom ArrayAdapter. Android basically doesn't know what's your layout's items, like the TextView. For this, you have another constructor where you can specify the TextView's ID:

final ArrayAdapter<String> aa=new ArrayAdapter<String>(this, R.id.listView1, R.id.your_textview_id, listData);

Comments

0

In ArrayAdapter(Context context, int resource, T[] objects) constructor 2nd parameter is layout resource to be inflate,so you have to pass R.layout instead of R.id as a argument.And in your layout there should be TextView of id @android:id/text so that android set your string automatically. If you want to give your id then use ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects) ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.