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

0

4 Answers 4

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 
Sign up to request clarification or add additional context in comments.

8 Comments

But I don't want to fill it with Strings. Can I not fill it with TextViews?
even in textview you need to settext with strings
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?
but how will you settext and id to arrayadapter and what the use of setting ids to textview in your case
With something like the following: TextView txtArtistName = new TextView(this); txtArtistName.setId(1); txtArtistName.setText("test"); arrayAdapter.add(txtArtistName);
|
0

Refer this links for usage of custom adapters.

LINK1

LINK2

LINK3

LINK4

LINK5

LINK6

Comments

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

4 Comments

While you are right, it's not that line that is returning the exception. It's the: lvSuggestions.setAdapter(arrayAdapter);
yes, lvSuggestions.setAdapter() will throw a NullPointerException if lvSuggestions is null
Ah, I understand. But it isn't null. I can see that in Debugging.
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
0

Maybe you put setContentView after setAdapter.

it must be like that:

setContentView(xxx);

//....

setAdapter(xxx)

Hope it help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.