0

I was wondering if it is possible to add subscript values to listview using a custom adapter. My adapter is expecting two List<String> and I'm using this method to populate my ListView. I looked around and a lot of people are recommending the

((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup>2</sup>"));

...but, I do not want the subscript applied to any additional list item that I add. I would only like it added to specific items/instance.

Fields.add("My Sample\nMy Sample\nMy Sample");
Values.add("Value\nValue\nValue");

enter image description here

For example I'd like to be able to pick and choose when it is applied:

enter image description here

Is this possible?

snapplex
  • 851
  • 3
  • 13
  • 27
  • Are you saying, you want the subscript applied not on all rows but on specific ones which satisfy some conditions ? – inmyth May 10 '15 at 02:45
  • @inmyth Yes, but the condition in this case is my deciding to apply subscript. – snapplex May 10 '15 at 03:22

1 Answers1

0

Rows in list view are associated with data in the adapter. To control the content of each row like applying subscript, you need to add necessary information in the data. So you cannot do it if your adapter uses solely List<String>. But if you change it to say a list of Item where Item is

class Item {
  String title;
  String subtitle;

}

and use List<Item> instead in the adapter. You can apply a subscript given subtitle is null or not in the adapter's getView

class MyAdapter extends ArrayAdapter<Item>{
    ...
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       // assuming proper view holder pattern is applied here          
       Item = getItem(position);
       if (item.subtitle != null){
          //apply subscript state
       }else{
          //un-apply subscript state
       }           
        return view;
    }

}
inmyth
  • 8,880
  • 4
  • 47
  • 52