我有以下CustomAdapter:
package com.test.testing;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class SetRowsCustomAdapter extends ArrayAdapter<SetRows> {
Context context;
int layoutResourceId;
ArrayList<SetRows> data=new ArrayList<SetRows>();
DateFormat df = new SimpleDateFormat("EEEEE, LLLL d", Locale.US);
String[] suspendedDates = {
"Monday, January 20",
"Friday, January 31",
};
public SetRowsCustomAdapter(Context context, int layoutResourceId, ArrayList<SetRows> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ImageHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ImageHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.tvDateVal);
//holder.txtTitle.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/robm.ttf"));
holder.imgIcon = (ImageView)row.findViewById(R.id.ivIcon0);
holder.txtDate = (TextView)row.findViewById(R.id.tvDateNum);
holder.txtID = (TextView)row.findViewById(R.id.tvReasonVal);
//holder.txtID.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/robm.ttf"));
row.setTag(holder);
}
else
{
holder = (ImageHolder)row.getTag();
}
SetRows myImage = data.get(position);
int inReason = myImage.name.indexOf(","); //myImage.name is the same string as suspendedDates[];
String strR = myImage.name.substring(0, inReason);
Spannable WordToSpan = new SpannableString(strR);
WordToSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#4787ED")), 0, WordToSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
String strRNext = myImage.name.substring(inReason, myImage.name.length());
Spannable WordToSpan1 = new SpannableString(strRNext);
WordToSpan1.setSpan(new ForegroundColorSpan(R.color.dateholiday), 0, WordToSpan1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
String strConcat = WordToSpan.toString() + WordToSpan1.toString();
holder.txtTitle.setText(strConcat);//myImage.name);
holder.txtID.setText(myImage.id);
holder.txtDate.setText(myImage.date);
int outImage=myImage.image;
/*if (myImage.name.contains(df.format(Calendar.getInstance(Locale.US).getTime()))) {
holder.imgIcon.setImageResource(R.drawable.caliconpressed);
}
else {
holder.imgIcon.setImageResource(R.drawable.calicon);
}*/
return row;
}
static class ImageHolder
{
ImageView imgIcon;
TextView txtTitle;
TextView txtID;
TextView txtDate;
}
}
我正在使用Spannable
设置字符串的单独颜色。它应该产生这样的东西:
但它仍在显示:
任何人都知道如何编辑适配器以实现我想要做的事情?
答案 0 :(得分:3)
使用spannable字符串如下:
SpannableString ss = new SpannableString("hey #abc how are you.");
ss.setSpan(new ForegroundColorSpan(Color.RED), 4, 9, 0);
//Now just add the SpannableString to your textview
textView.setText(ss);
希望这有助于。
问候!
答案 1 :(得分:3)
不要将Spannable转换为String。 (即不要做:WordToSpan.toString()
)
相反,将Spannable直接设置为持有者,如下所示:
holder.txtTitle.setText(WordToSpan + WordToSpan1);