Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How put <Spanned> into list.setAdapter?

Simple but little tricky, if I have

list.setAdapter(new ArrayAdapter<String>(this,R.layout.double_row, R.id.doubleRow, articleItemsHelper));

it works if articleItemsHelper is String, but I wanna have HTML formatting in there so when articleItemsHelper is type Spanned this (adapter) doesn't work.

ArrayList<Spanned> articleItemsHelper = new ArrayList<Spanned>();

What's the solution?

EDIT: here is the solution - custom adapter

private static class SpannedAdapter extends BaseAdapter {
     private LayoutInflater mInflater;
     private ArrayList<Spanned> mArticleList;

     public SpannedAdapter(Context context, ArrayList<Spanned> articleList) {
        mInflater = LayoutInflater.from(context);
        mArticleList = articleList;
     }

     public int getCount() {
         return mArticleList.size();
     }

     public Object getItem(int position) {
         return position;
     }

     public long getItemId(int position) {
         return position;
     }

     public View getView(int position, View convertView, ViewGroup parent) {
         ViewHolder holder;
         if (convertView == null) {
             convertView = mInflater.inflate(R.layout.single_row, null);
             holder = new ViewHolder();
             holder.text = (TextView) convertView.findViewById(R.id.singleRow);

             convertView.setTag(holder);
         } else {
             holder = (ViewHolder) convertView.getTag();
         }

         holder.text.setText(mArticleList.get(position));

         return convertView;
     }

     static class ViewHolder {
         TextView text;
     }
}

Then just regularly call

list.setAdapter(new SpannedAdapter(this, articleItemsHelper));

where

articleItemsHelper

is

ArrayList<Spanned>
like image 632
svenkapudija Avatar asked Nov 15 '22 04:11

svenkapudija


1 Answers

Old thread but I found an other way to do it, it can help people:

    simpleAdpt = new ArrayAdapter<Note>(this, R.layout.notelist, listeNotes ){
            public View getView(int position, View view, ViewGroup viewGroup)
            {
                View v = super.getView(position, view, viewGroup);
                Note n = (Note) this.getItem(position);
                ((TextView)v).setText(Html.fromHtml("<b>"+n.getTitre() + "</b> <br/>"+n.getNote()));
                 return v;
            }
    };
like image 130
Kelevra Avatar answered Dec 09 '22 20:12

Kelevra