Aditya Vyas Aditya Vyas - 6 months ago 12
JSON Question

How to set color code as per Json Object

I have following Json Response,I am trying to set color as per DisplyText,but it only set last color code to whole String,

JSON Response

JAVA Code

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

}


}


Adapter

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

}

Answer

You can try the following method. Format your text as HTML and use it for the display.

private String addColor(String str, String hexColor)
{
    return "<font color='"+hexColor+"'>"+str+"</font>";
}

Delete sb.append(disptext); from where it is right now and add sb.append(addColor(disptext, hex)); after hex = String.format("#%02X%02X%02X", part1, part2, part3);

Replace dettext.setText(detailtext); with dettext.setText(Html.fromHtml(detailtext)); and delete dettext.setTextColor(Color.parseColor(hex));.

This should do the job.

Also, you are not using your ArrayLists ch_list and color_list which can also be used for this task.

Comments