如何根据 Json 对象设置颜色代码

How to set color code as per Json Object

我有以下 Json 响应,我正在尝试根据 DisplyText 设置颜色,但它只将最后一个颜色代码设置为整个字符串,

JSON Response

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

    }

要更改文本视图中特定单词的颜色而不更改整个颜色,您将不得不使用

 String noColor = "I like the ";
 String redColor = "<font color='#EE0000'>color red</font>";
 yourTextView.setText(Html.fromHtml(noColor + redColor));

只需使用它并根据您的需要进行更改。将您的字符串拆分为更小的字符串,并根据需要为每个字符串应用颜色,然后使用 Html.fromHtml

合并它们

您可以试试下面的方法。将您的文本格式化为 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);并在hex = String.format("#%02X%02X%02X", part1, part2, part3);之后添加sb.append(addColor(disptext, hex));

dettext.setText(detailtext);替换为dettext.setText(Html.fromHtml(detailtext));并删除dettext.setTextColor(Color.parseColor(hex));

这应该可以完成工作。

此外,您没有使用您的 ArrayLists ch_listcolor_list,它们也可用于此任务。