0

I got this piece of code that creates new TextView, then adds it to ArrayList<View> and when it is finished adding TextViews to Array it sets adds that Array into ListView. But somehow my ListView is appearing empty. Any idea what am I doing wrong?

Here is the code:

ListView lv = (ListView) findViewById(R.id.listView1); ArrayList<View> textvs = new ArrayList<View>(); for (int i=0; i<10;i++) { TextView tv = new TextView(MainActivity.this); tv.setText(""+i); textvs.add(tv); } lv.addTouchables(portit); // lv is my listview 
3
  • Is populating the list view what you wanna achieve ? Commented Aug 11, 2012 at 11:30
  • Read this post to understand the concept of listview vogella.com/articles/AndroidListView/article.html Commented Aug 11, 2012 at 11:34
  • Do you need to call setContentView()? Commented Jun 16, 2013 at 2:10

1 Answer 1

1

You should use ArrayAdapter. You did this the wrong way. Here is a sample:

public class ArrayAdapterDemo extends ListActivity { String[] items = { "this", "is", "a", "really", "silly", "list" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, items)); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.