1

我有带有文本和图像的自定义列表视图,从 string.xml 中的 html 字符串检索的每一行文本,

所以每一行都必须显示文本和图像,但是当我运行应用程序时它不显示 html 标签,它会获取存储在 string.xml 中的文本并在每一行中显示它但没有 html 标签效果,图像也不显示在每一行

我们如何让它最终显示列表行可能包含带有 html 标签和图像的文本。

任何建议将不胜感激。

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
     ListView listView = (ListView)findViewById(R.id.lis);
     String[] textEntries = {getString(R.string.day1),getString(R.string.day2), 
                             getString(R.string.day3),getString(R.string.day4),
                             getString(R.string.day5)};

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, textEntries);
 listView.setAdapter(adapter);
    }
}

适配器类

public class MyAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] textEntries;
Typeface tf;
static class ViewHolder {
    public TextView text;
    public ImageView image;
    }     
public MyAdapter(Activity context, String[] textEntries) {
    super(context, R.layout.row, textEntries);
    this.context = context;
    this.textEntries = textEntries;
    tf=Typeface.createFromAsset(context.getAssets(),"BFantezy.ttf");
    }                                                   
@Override
public View getView(int position, View convertView, ViewGroup parent) {     
    View rowView = convertView;
    if (rowView == null) {
      LayoutInflater inflater = context.getLayoutInflater();
      rowView = inflater.inflate(R.layout.row, null);
      ViewHolder viewHolder = new ViewHolder();
      viewHolder.text = (TextView) rowView.findViewById(R.id.row_label);
      viewHolder.image = (ImageView) rowView.findViewById(R.id.row_image);
      viewHolder.text.setTypeface(FontFactory.getBFantezy(getContext()));
      rowView.setTag(viewHolder);
      }     
     ViewHolder holder = (ViewHolder) rowView.getTag();
     String s = textEntries[position];
     holder.text.setText(s);

if ( s.startsWith("day1")) {
    holder.image.setImageResource(R.drawable.a); 
    }                       
if ( s.startsWith("day2")) {
    holder.image.setImageResource(R.drawable.b); 
    }       
if ( s.startsWith("day3")) {
    holder.image.setImageResource(R.drawable.c); 
    }                       
if ( s.startsWith("day4")) {
    holder.image.setImageResource(R.drawable.d); 
    }       
if ( s.startsWith("day5")) {
    holder.image.setImageResource(R.drawable.e); 
    }                       
return rowView;  
}
}
4

0 回答 0