0

I'm trying to set the contents of a ListView to an array of TextViews.

With what I have currently I get a Null Pointer Exception, even though I think I'd doing what is suggested here: how do I add items to listview dynamically in android

Here's what I have currently:

ListView lvSuggestions = (ListView)findViewById(R.id.lvSuggestions);

ArrayAdapter<TextView> arrayAdapter =  new ArrayAdapter<TextView>(this, android.R.layout.simple_list_item_1);
lvSuggestions.setAdapter(arrayAdapter);

But I get the exception when it sets the adaptor.

I've tried filling the ArrayList with TextViews and then creating the Adaptor with the full ArrayList, but I get the same Null Pointer Exception when I set the Adaptor.

Thanks for any help

Community
  • 1
  • 1
Tom Bowen
  • 8,214
  • 4
  • 22
  • 42

4 Answers4

1
ArrayAdapter<TextView> arrayAdapter =  new ArrayAdapter<TextView>(this, android.R.layout.simple_list_item_1);

change the above line with below

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1);

and add all the string that you want to display to arrayAdapter as below

arrayAdapter.add("string1");
arrayAdapter.add("string2");
arrayAdapter.add("string3");//so on 
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
  • But I don't want to fill it with Strings. Can I not fill it with TextViews? – Tom Bowen Apr 15 '12 at 15:50
  • even in textview you need to settext with strings – Shankar Agarwal Apr 15 '12 at 15:51
  • Yeah, I know I would need to set the Textview text with strings. But I want my ListView to contain an array of TextViews, because I want to be able to set the id's of the TextViews. Is it possible to fill a ListView with an ArrayList of type TextView? – Tom Bowen Apr 15 '12 at 15:54
  • but how will you settext and id to arrayadapter and what the use of setting ids to textview in your case – Shankar Agarwal Apr 15 '12 at 15:58
  • With something like the following: TextView txtArtistName = new TextView(this); txtArtistName.setId(1); txtArtistName.setText("test"); arrayAdapter.add(txtArtistName); – Tom Bowen Apr 15 '12 at 16:00
  • posted an one more answer go through that links. – Shankar Agarwal Apr 15 '12 at 16:03
  • @Tom: Agarwal's answer is the correct way use an `ArrayAdapter` with `android.R.layout.simple_list_item_1` which (for all intents and purposes) simply defines a `TextView` per list item. There is no need to have a different `id` for each `TextView` in this case - if a user selects (clicks) the list view item you can simply retrieve the text from there. – Squonk Apr 15 '12 at 16:10
  • I wanted to set the id, as I have "hidden" text that I would like to retrieve on click. It's a unique key for the text that is displayed. And I would like to know which key has been clicked. – Tom Bowen Apr 15 '12 at 20:19
0

This

(ListView)findViewById(R.id.lvSuggestions);

might return null if findViewById returns null (no item is found) or the returned object is not of type ListView.

Protect it with:

if (lvSuggestions != null) {
  ArrayAdapter<TextView> arrayAdapter = new ArrayAdapter<TextView>(this, 
      android.R.layout.simple_list_item_1);
  lvSuggestions.setAdapter(arrayAdapter);
}
Attila
  • 28,265
  • 3
  • 46
  • 55
  • While you are right, it's not that line that is returning the exception. It's the: lvSuggestions.setAdapter(arrayAdapter); – Tom Bowen Apr 15 '12 at 15:48
  • yes, `lvSuggestions.setAdapter()` will throw a NullPointerException if `lvSuggestions` is `null` – Attila Apr 15 '12 at 15:49
  • Ah, I understand. But it isn't null. I can see that in Debugging. – Tom Bowen Apr 15 '12 at 15:52
  • Then go with Agarval's answer (I'm not knowledgable enough with the inner workings of androuid :)), but I suggest you put the protective if there anyway: can save you from future problems – Attila Apr 15 '12 at 15:59
0

Refer this links for usage of custom adapters.

LINK1

LINK2

LINK3

LINK4

LINK5

LINK6

Community
  • 1
  • 1
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
0

Maybe you put setContentView after setAdapter.

it must be like that:

setContentView(xxx);

//....

setAdapter(xxx)

Hope it help!

Lạng Hoàng
  • 1,790
  • 3
  • 17
  • 32