ArrayAdapter in android to create simple listview

AndroidListviewAndroid Arrayadapter

Android Problem Overview


I tried to create an Activity in Android, This Activity only contains a ListView nothing else.

As I know to fill the listview we need to use an ArrayAdapter.

So to understand the ArrayAdapter I have read the following link:

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

But still I am unable to understand it clearly!

One of the biggest doubt is why the constructor needs a TextView resource id while my activity is not having any TextViews what I should have to give it?

I am not saying that this is the only constructor, just that I'm unable to understand the logic behind it.

In order to create a simple listview I also referred to the following link:

Simple ListView using ArrayAdapter example.

But again my main doubt is why it does it need a TextView resource id?

If anybody can explain it with an example it will be very helpful.

EDIT:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
          android.R.layout.simple_list_item_1, android.R.id.text1, values);

Android Solutions


Solution 1 - Android

ArrayAdapter uses a TextView to display each item within it. Behind the scenes, it uses the toString() method of each object that it holds and displays this within the TextView. ArrayAdapter has a number of constructors that can be used and the one that you have used in your example is:

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

By default, ArrayAdapter uses the default TextView to display each item. But if you want, you could create your own TextView and implement any complex design you'd like by extending the TextView class. This would then have to go into the layout for your use. You could reference this in the textViewResourceId field to bind the objects to this view instead of the default.

For your use, I would suggest that you use the constructor:

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

In your case, this would be:

ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values)

and it should be fine. This will bind each string to the default TextView display - plain and simple white background.

So to answer your question, you do not have to use the textViewResourceId.

Solution 2 - Android

> But again main doubt why TextView resource id it needs?

Look at the constructor and the params.

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

> Added in API level 1 Constructor > > Parameters

> context The current context.

> resource The resource ID for a > layout file containing a layout to use when instantiating views.

> textViewResourceId The id of the TextView within the layout resource > to be populated objects The objects to represent in the ListView.

android.R.id.text1 refers to the id of text in android resource. So you need not have the one in your activity.

Here's the full list

http://developer.android.com/reference/android/R.id.html

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, android.R.id.text1, values);

this refers to activity context

android.R.layout.simple_list_item_1 simple_list_item_1 is the layout in android.R.layout.

android.R.id.text1 refers to the android resource id.

values is a string array from the link you provided

http://developer.android.com/reference/android/R.layout.html

Solution 3 - Android

The TextView resource id it needs is for a TextView layout file, so it won't be in the same activity.

You can create it by going to File > New > XML > XML Layout File, and enter the widget type, which is 'TextView' in the root tag field.

Source: https://www.kompulsa.com/the-simplest-way-to-implement-an-android-listview/

Solution 4 - Android

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

Here, resource means the 'id' of the Layout you are using while instantiating the view.

Now, this layout has many child views with their own ids. So, textViewResourceId tells which child view we need to populate with the data.

Solution 5 - Android

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

I am also new to Android , so i might be wrong. But as per my understanding while using this for listview creation 2nd argument is the layout of list items. A layout consists of many views (image view,text view etc). With 3rd argument you are specifying in which view or textview you want the text to be displayed.

Solution 6 - Android

You don't need to use id for textview. You can learn more from android arrayadapter. The below code initializes the arrayadapter.

ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.single_item, eatables);

Solution 7 - Android

For your question answer is android.R.id.text1 is int: The id of the TextView within the layout resource to be populated.

ArrayAdapter has so many constructors with different number of arguments I'm mention some of them

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

Now you can understand each and every constructor is different and they used different list of arguments.

And simple answer is you can use ArrayAdapter with text view inside a target xml file or without. It is doesn't matter. And you not need specify text view id you can use it without. But you may need to go with some advance option with your simple list view you must go with a text view.!

Here sample example

ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.ListView,StringArray);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);

This is also a valid code you can use with much more clear.

Solution 8 - Android

If you have more than one view in the layout file android.R.layout.simple_list_item_1 then you'll have to pass the third argument android.R.id.text1 to specify the view that should be filled with the array elements (values). But if you have just one view in your layout file, there is no need to specify the third argument.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNirav KamaniView Question on Stackoverflow
Solution 1 - AndroiducsunilView Answer on Stackoverflow
Solution 2 - AndroidRaghunandanView Answer on Stackoverflow
Solution 3 - AndroidNicholas BrownView Answer on Stackoverflow
Solution 4 - AndroidPrashant KView Answer on Stackoverflow
Solution 5 - AndroidAnuj MahajanView Answer on Stackoverflow
Solution 6 - AndroidRajat GhaiView Answer on Stackoverflow
Solution 7 - AndroidSahan Pasindu NirmalView Answer on Stackoverflow
Solution 8 - AndroidHimanshu SinghView Answer on Stackoverflow