Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayAdapter in android to create simple listview

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); 
like image 700
Nirav Kamani Avatar asked Sep 29 '13 14:09

Nirav Kamani


People also ask

What is the use of ArrayAdapter in Android?

You can use this adapter to provide views for an AdapterView , Returns a view for each object in a collection of data objects you provide, and can be used with list-based user interface widgets such as ListView or Spinner .

What does an ArrayAdapter do?

ArrayAdapter is an Android SDK class for adapting an array of objects as a datasource. Adapters are used by Android to treat a result set uniformly whether it's from a database, file, or in-memory objects so that it can be displayed in a UI element. The ArrayAdapter is useful for the latter.

What is ListView adapter in Android?

BaseAdapter with Android ListView. ListView is a ViewGroup that displays a list of vertically scrollable items. The list items are automatically inserted into the list using an adapter that is connected to a source, such as an array or a database query, and each item is converted into a row in the ListView.


2 Answers

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.

like image 122
ucsunil Avatar answered Oct 16 '22 02:10

ucsunil


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

like image 34
Raghunandan Avatar answered Oct 16 '22 00:10

Raghunandan