【问题标题】:How to set color code as per Json Object如何根据 Json 对象设置颜色代码
【发布时间】:2016-05-25 06:07:13
【问题描述】:

我有以下 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;
        }

    }

【问题讨论】:

  • dettext.setTextColor(Color.parseColor(hex)); 中的hex 的值是多少?如我所见,这将是最后一种颜色的值?不使用ArrayLists ch_listcolor_list 有什么意义?

标签: android json listview colors baseadapter


【解决方案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);,并在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,它们也可用于此任务。

【讨论】:

  • 你能说一件事吗?
  • 是的,你想知道什么?
  • 有了你的回答,我可以设置很好的颜色,但是我如何在下一行“\\nHurry While Stocks Last!”中设置这个文本。我想在下一个设置行
  • 嗨。编辑了答案。
  • 仍然无法正常工作,它显示如下输出:购买 6 包 Get2 Dose FreeHurry 售完即止
【解决方案2】:

要在 textview 中更改特定单词的颜色,而不是更改您必须使用的整个颜色

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-21
    • 2014-12-12
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多