Skip to content Skip to sidebar Skip to footer

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:

publicclassMyAdapterextendsArrayAdapter<Address> {

    Context context; 
    int layoutResourceId;    

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

    @Overridepublic View getView(int position, View convertView, ViewGroup parent) {
        Viewrow= convertView;
        StringHolderholder=null;

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

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

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

        AddressaddressItem= getItem(position);
        Spannedformat= Html.fromHtml("<br/>" + addressItem.getAddress() + "<br/>" +  addressItem.getName() + "<br/>");

        holder.txtTitle.setText(format);

        return row;
    }

    staticclassStringHolder
    {
        TextView txtTitle;
    }
}

Then use it onCreate as follows:

List<Address> values = datasource.getAllAddresses();
MyAdapteradapter=newMyAdapter(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"