本文介绍了Android开发人员:试图款式/颜色与ArrayAdapter生成的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法颜色/结果字符串的风格某些部分?

Is there a way to color/style certain portions of the resulting string?

我有一个ListView布局,并为相应的活动下面onCreate方法

I have a ListView layout, and the following onCreate method for the corresponding Activity.

public class AddressesActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addresses);

    AddressDBHandler datasource = new AddressDBHandler(this);
    datasource.open();

    List<Address> values = datasource.getAllAddresses();
    ArrayAdapter<Address> adapter = new ArrayAdapter<Address>(this,
        android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
}

    .
    .
    .

的数据是从下面的类加载:

The data is loaded from the following class:

public List<Address> getAllAddresses() {
    List<Address> addresses = new ArrayList<Address>();

    Cursor cursor = database.query(MySQLiteHelper.TABLE_ADDRESSES,
        allColumns, null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      Address address = cursorToAddress(cursor);
      addresses.add(address);
      cursor.moveToNext();
    }
    cursor.close();
    return addresses;
}

在Address类

我的toString()方法如下:

My toString() method in the Address class is the following:

      // Will be used by the ArrayAdapter in the ListView
  @Override
  public String toString() {
    Spanned format = Html.fromHtml("<br/>" + address + "<br/>" + name + "<br/>");
    return format.toString();
  }

好像我正在用break标签结果。我只想地址是从名称不同的颜色。

Seems like I am getting results with the break tags. I would just like the address to be a different color from the name.

推荐答案

您应该创建一个适配器类如下:

You should create a adapter class as follows:

public class MyAdapter extends ArrayAdapter<Address> {

    Context context;
    int layoutResourceId;

    public MyAdapter(Context context, int layoutResourceId, List<Address> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        StringHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new StringHolder();
            holder.txtTitle = (TextView)row.findViewById(R.id.text1);

            row.setTag(holder);
        }
        else
        {
            holder = (StringHolder)row.getTag();
        }

        Address addressItem = getItem(position);
        Spanned format = Html.fromHtml("<br/>" + addressItem.getAddress() + "<br/>" +  addressItem.getName() + "<br/>");

        holder.txtTitle.setText(format);

        return row;
    }

    static class StringHolder
    {
        TextView txtTitle;
    }
}

然后使用的onCreate 如下:

List<Address> values = datasource.getAllAddresses();
MyAdapter adapter = new MyAdapter(this, android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);

这篇关于Android开发人员:试图款式/颜色与ArrayAdapter生成的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 12:52