我有跟随Json Response,我正在尝试根据DisplyText设置颜色,但它只将最后颜色代码设置为整个String,
JAVA代码
ch_list = new ArrayList<String>();
color_list=new ArrayList<String>();
try {
for (int i = 0; i < response.length(); i++) {
JSONObject person = (JSONObject) response
.get(i);
System.out.println("person"+person);
String searchcode = person.getString("searchCode");
System.out.println("searchcode"+searchcode);
JSONArray ja = person.getJSONArray("itemList");
for (int j = 0; j < ja.length(); j++) {
JSONObject jo = ja.getJSONObject(j);
SearchResultModel ch = new SearchResultModel();
ch.setSearch_Name(jo.getString("productName"));
ch.setSearch_Img(jo.getString("productImage"));
ch.setSearch_ImgLoc(jo.getString("productImageLoc"));
ch.setSearch_Price(jo.getString("productSP"));
ch.setSearch_RatingImg(jo.getString("pRatingImgName"));
ch.setSearch_RatingImgPath(jo.getString("pRatingImgPath"));
JSONArray txtdetail=jo.getJSONArray("productOfferText");
System.out.println("txtdetailarray"+txtdetail);
StringBuilder sb = new StringBuilder();
for(int k=0;k<txtdetail.length();k++)
{
JSONObject jdetail=txtdetail.getJSONObject(k);
disptext=jdetail.getString("displayText");
dispclr=jdetail.getString("displayColor");
ch_list.add(disptext);
color_list.add(dispclr);
sb.append(disptext);
System.out.println("clr" + color_list.get(k).toString());
colors=color_list.get(k);
String string = colors;
String[] parts = string.split("\\*");
int part1 = Integer.parseInt(parts[0]);
int part2 = Integer.parseInt(parts[1]);
int part3 = Integer.parseInt(parts[2]);
hex = String.format("#%02X%02X%02X", part1, part2, part3);
System.out.println("hexa"+hex);
// System.out.println("textnames" + ch_list.get(k).toString());
}
detailtext=sb.toString().replaceAll("\\\\n", "\n");
// System.out.println("gh"+sb.toString()+detailtext);
System.out.println("Output: " + sb.toString().replaceAll("\\\\n", "\n"));
searchlist.add(ch);
}
}
适配器
public class CustomListAdapterCountry extends BaseAdapter {
private AQuery aQuery;
private Activity activity;
private LayoutInflater inflater;
private List<SearchResultModel> movieItems;
private List<String> DispItems;
ImageLoader imageLoader = MyApplication.getInstance().getImageLoader();
public CustomListAdapterCountry(Activity activity, List<SearchResultModel> movieItems,ArrayList<String> DispItems) {
this.activity = activity;
this.movieItems = movieItems;
this.DispItems = DispItems;
aQuery = new AQuery(this.activity);
}
@Override
public int getCount() {
return movieItems.size();
}
@Override
public Object getItem(int location) {
return movieItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_item_searchresult, null);
if (imageLoader == null)
imageLoader = MyApplication.getInstance().getImageLoader();
NetworkImageView iv = (NetworkImageView) convertView
.findViewById(R.id.search_image);
ImageView ratingiv = (ImageView) convertView
.findViewById(R.id.search_rating);
TextView title = (TextView) convertView.findViewById(R.id.search_title);
TextView price = (TextView) convertView.findViewById(R.id.search_price);
TextView dettext = (TextView) convertView.findViewById(R.id.search_detailtext);
// getting movie data for the row
SearchResultModel m = movieItems.get(position);
iv.setImageUrl(m.getSearch_ImgLoc() + m.getSearch_Img(), imageLoader);
aQuery.id(ratingiv).image(m.getSearch_RatingImgPath() + m.getSearch_RatingImg(), true, true, 0, R.mipmap.ic_launcher);
//Log.d("searchimgs", joined);
// title
title.setText(m.getSearch_Name());
price.setText("$"+m.getSearch_Price());
dettext.setText(detailtext);
dettext.setTextColor(Color.parseColor(hex));
return convertView;
}
}
答案 0 :(得分:1)
您可以尝试以下方法。将文本格式化为HTML并将其用于显示。
private String addColor(String str, String hexColor)
{
String html = "";
if(str.contains("\\\\n"))
{
html = "</br>";
str.replaceAll("\\\\n", "");
}
html = html + "<font color='"+hexColor+"'>"+str+"</font>";
return html;
}
从现在的位置删除sb.append(disptext);
并在sb.append(addColor(disptext, hex));
之后添加hex = String.format("#%02X%02X%02X", part1, part2, part3);
将dettext.setText(detailtext);
替换为dettext.setText(Html.fromHtml(detailtext));
并删除dettext.setTextColor(Color.parseColor(hex));
。
这应该可以胜任。
此外,您还没有使用ArrayList
ch_list
和color_list
,它们也可用于此任务。
答案 1 :(得分:0)
要在textview中更改特定单词的颜色而不更改您必须使用的整个颜色
String noColor = "I like the ";
String redColor = "<font color='#EE0000'>color red</font>";
yourTextView.setText(Html.fromHtml(noColor + redColor));
只需使用它并改变您的需求。将您的字符串拆分为较小的字符串,并根据需要为每个字符串添加颜色,然后使用Html.fromHtml
合并它们