2
votes

I have a custom ArrayAdapter set to a listView. Inside each element of the listView I have an item that matches a .xml I created. Codes shown below:

listContent.xml

<RelativeLayout
[... some code ommited]
    <ImageView
    [...]
    <TextView
    android:id="@+id/listViewText"
    [...]

Custom Array Adapter getView(int position, View convertView, ViewGroup parent)

if (position < 4)
{
    convertView = inflater.inflate(R.layout.listContent,null);

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

    tv.setTextSize(18);
    if (position == 0) tv.setText("0");
    else if (position == 1) tv.setText("1");
    else if (position == 2) tv.setText("2");
    else if (position == 3) tv.setText("3");

    return convertView;
}
return null;

I can't seem to get this to work, I always get some error. What do I need to do in order to edit the TextView inside my listView item?

2
define 'some error', and post relevant stacktrace. Plus, use convertView.findById, as findById is not defined for an ArrayAdapter - njzk2

2 Answers

1
votes

Change your Custom Array Adapter getView code as:

@Override
public View getView(int position, View convertView, ViewGroup parent){

 View row = convertView;

 if(row==null){
  LayoutInflater inflater=getLayoutInflater();
  row=inflater.inflate(R.layout.listContent, parent, false);
 }

 TextView tv=(TextView)row.findViewById(R.id.listViewText);


tv.setTextSize(18);
    if (position == 0) tv.setText("0");
    else if (position == 1) tv.setText("1");
    else if (position == 2) tv.setText("2");
    else if (position == 3) tv.setText("3");

 return row;
}
1
votes

Change

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

to

TextView tv = (TextView) convertView.findViewById(R.id.listViewText);

and remove this

if (position < 4)