0

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 :)

1 Answer 1

1

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

5 Comments

but i need it to have an ID so later on i can change that field in particular!
Each row in an adapter has a unique ID (generally its position). You can get this by calling long id = adapter.getItemId(position);
Thanks! how can i add subtext like this?
Using the generic implementation of ArrayAdapter it's not possible. ArrayAdapter only allows you to set 1 field. You'd need to implement your own custom Adapter at this point
Here's a pretty good example of how to make a custom adapter android.vexedlogic.com/2011/04/02/…

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.