Style first 2 TextViews in Android ListView differently

Posted by Kurtis Nusbaum on Stack Overflow See other posts from Stack Overflow or by Kurtis Nusbaum
Published on 2010-12-21T05:14:46Z Indexed on 2010/12/21 5:21 UTC
Read the original article Hit count: 223

Filed under:
|
|

I have a ListView and I want the first two entries in it to be displayed differently than the rest. Nothing fancy, I want them all to just be text views. But the first two entries need to have different sizes and weights than the rest. I tried modifying the ArrayAdapter class like so:

private class BusStopAdapter<T> extends ArrayAdapter<T>{

  public BusStopAdapter(  
    Context context, int textViewResourceId,
    List<T> objects)
  {
    super(context, textViewResourceId, objects);
  }

  public View getView(int position, View convertView, 
    ViewGroup parent)
  {
    TextView toReturn = 
      (TextView)super.getView(position, convertView, parent);
    if(position == 0){
     toReturn.setTextSize(12);
     toReturn.setText("Previous Bus: " + toReturn.getText());
     toReturn.setPadding(0,0,0,0);
    }
    else if(position == 1){
      toReturn.setTextSize(20);
      toReturn.setPadding(
        toReturn.getPaddingLeft(),
        0,
        toReturn.getPaddingRight(),
        0
      );
      toReturn.setText("Next Bus: " + toReturn.getText());
      toReturn.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.TOP);
    }
    return toReturn;
  }

}

But this inadvertantly causes some of the other textviews to take on these special attributes. I think it's because cause textviews get "recycled" in the AbsListAdapter class.

© Stack Overflow or respective owner

Related posts about android

Related posts about listview