0

I have a list and I would like to dynamically add rows to this list, but I want every row to be a TextView. Is this possible?

The following code doesn't work because it tells me that the method addView(TextView) is undefined for the type ArrayList (listItems):

TextView textView = new TextView(this);
textView.setText("sample text");
listItems.addView(textView);

Which is the correct way to add text views to a list then?

user1301428
  • 1,743
  • 3
  • 25
  • 57
  • Check this link: http://stackoverflow.com/questions/1917773/dynamic-listview-in-android-app – Bhavin Apr 01 '12 at 17:52
  • I've read the article, but I don't know if I understood how it can be applied to my question: basically it says that if I want to add a TextView I should say so in the private class RowData? – user1301428 Apr 01 '12 at 18:35

1 Answers1

1

The link in the comment is very helpful.

But to explain here, basically, you don't add textViews to the ArrayList, you add elements to the ArrayList and the listAdapter will automatically put time into the listView. Also, you might need to call invalidate on the view to force it to update.

dcow
  • 7,765
  • 3
  • 45
  • 65
  • That's exactly what I am doing right now: I am adding strings to the ArrayList, I was wondering if it was better to add text views instead :) – user1301428 Apr 01 '12 at 18:32
  • I was wondering: if, like you say, I add an element to a ListView, is this automatically transformed into a view? – user1301428 Apr 01 '12 at 18:45