rel-s
rel-s

Reputation: 6138

Android - Adding TextViews to a ListView Dynamically

So i am trying to add TextView's into my main ListView within JAVA, And i am encountering some problems...

instead of giving me the Value of the TextView, all i get is some random rubbish like:

 android.Widget.TextView@somerandomnumbersandtext

I am adding the textview like this:

        adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
    setListAdapter(adapter);
    TextView tv = new TextView(SpellCast.this);
    TextView tvC = new TextView(SpellCast.this);
    tv.setText(name[i]);
    tvC.setText(Integer.toString(current[i]));
    tvC.setId(i);
    tv.setGravity(Gravity.LEFT);
    tvC.setGravity(Gravity.RIGHT);
    listItems.add(tv + "");
    adapter.notifyDataSetChanged();

Why isn't this working??

thanks :)

Upvotes: 0

Views: 2030

Answers (1)

dymmeh
dymmeh

Reputation: 22306

You should be adding a string and not a textview.

When you call adapter=new ArrayAdapter<String> you are saying "I'm defining a new ArrayAdapter that will contain items of the type 'String' "

So, when you try to add an item to your listview it is expecting a string. When you add a textview it is simply performing a toString() on the textview which is why you get that funky text.

Instead, add your text directly to the listview by calling

listItems.add(name[i]);

Upvotes: 1

Related Questions