Android Dev: Attempting To Style/color Resulting String With ArrayAdapter
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 Add
Solution 1:
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);
Post a Comment for "Android Dev: Attempting To Style/color Resulting String With ArrayAdapter"