Search code examples
androideclipsetextviewwidthlistadapter

android eclipse dynamically set list adapters child TextView width


I have a list that is populated by a listadapter that specifies a layout like so:

SimpleCursorAdapter trips = 
new SimpleCursorAdapter(this, R.layout.it_menu_home_row, tripsCursor, from, to);

Inside of that layout is a textview. I would like to be able to dynamically change the width of that textview through code. I have tried just referencing the textview ID with findViewById, but when I try to manipulate it, I get a null pointer exception.

Is there a simple way reference the textview of a listadapters layout?

I am trying to reference the text view like I would if it was sitting in the base layout of the activity:

TextView tv = (TextView)findViewById(R.id.myTV);

I don't know if I have to somehow iterate through each of the list items and manually set the textView that way (and if I do I am not sure how), or if I just have to reference it once, etc


Solution

  • When you use SimpleCursorAdapter you provide the textFields via the constructor itself. In the snippet you have given, to should be an array of id's of the TextView's present. For example if your layout file has a TextView with the id: "@+id/my_textfield" then you should pass in your constructor an array: int to[]=new int[]{R.id.my_textfield};