如何将<spanned>放入list.setAdapter?</spanned>

时间:2011-01-02 21:39:17

标签: android list listview arraylist spanned

简单但有点棘手,如果我有

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

如果 articleItemsHelper String ,它可以工作,但我想在那里有HTML格式,所以当 articleItemsHelper 跨越时b>这个(适配器)不起作用。

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

解决方案是什么?

编辑:这是解决方案 - 自定义适配器

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;
     }
}

然后定期打电话

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

,其中

articleItemsHelper

ArrayList<Spanned>

2 个答案:

答案 0 :(得分:0)

这是ArrayAdapter设置行文本的方式:

    T item = getItem(position);
    if (item instanceof CharSequence) {
        text.setText((CharSequence)item);
    } else {
        text.setText(item.toString());
    }

正如您所看到的,在您的情况下它会做什么是调用toString方法,这就是为什么它不起作用。所以,继续编写自己的适配器,在这种情况下你别无选择。

答案 1 :(得分:0)

旧线程,但我找到了另一种方法,它可以帮助人们:

    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;
            }
    };