Android Dev: Attempting to style/color resulting s

2019-09-02 12:45发布

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

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

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

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

1条回答
看我几分像从前
2楼-- · 2019-09-02 13:28

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

Then use it onCreate as follows:

List<Address> values = datasource.getAllAddresses();
MyAdapter adapter = new MyAdapter(this, android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
查看更多
登录 后发表回答