5

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

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

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

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>
4

2 回答 2

1

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

    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;
            }
    };
于 2014-07-15T19:16:10.250 回答
0

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

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

如您所见,在您的情况下它会调用该toString方法,这就是它不起作用的原因。所以,继续写你自己的适配器,在这种情况下你别无选择。

于 2011-01-02T21:47:54.487 回答