Gunah Gaar
Gunah Gaar

Reputation: 535

android list view failed to bind array

Each listView binding tutorial I checked but I have a strong confusion in the adapter section. First see my code

ListView listView = (ListView) findViewById(R.id.mylist);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
  "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
  "Linux", "OS/2" };

// First paramenter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
  android.R.layout.simple_list_item_1, android.R.id.text1, values);

// Assign adapter to ListView
listView.setAdapter(adapter); 

See this line

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

What is this? android.R.layout.simple_list_item_1 who says that we can refer to the listeView by R???

why??? Please help

Upvotes: 0

Views: 368

Answers (3)

Anoop
Anoop

Reputation: 993

use the following code....

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,

android.R.layout.simple_list_item_1, values);

Upvotes: 0

G_S
G_S

Reputation: 7110

instead of

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

try using

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

or use

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

Upvotes: 0

ixx
ixx

Reputation: 32273

Check the docs:

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

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.

http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter(android.content.Context, int, int, T[])

You should have in the layout file simple_list_item_1.xml a textview with id text1.

Upvotes: 1

Related Questions