Vinit Shandilya
Vinit Shandilya

Reputation: 1643

Array adapter in android

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!

Upvotes: 0

Views: 155

Answers (4)

Ankur Samarya
Ankur Samarya

Reputation: 229

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

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

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

Upvotes: 2

nKn
nKn

Reputation: 13761

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

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157447

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

Upvotes: 1

Related Questions