Aditya Vyas Aditya Vyas - 5 months ago 26
JSON Question

How to set color code from json response

I am getting color code from server for my product listing,the color codes getting perfectly,but i am trying to set it in my

textview
but not able to set the
color
,following is my snippet code and
adapter
,any help will be appreciated.

The color code which i get from server

[
{
"colors": [
"#000000",
"#7E3517",
"#C85A17"
],

},
{
"
"colors": [
"#000000",
"#C85A17"
],

}
]


MainActivity.java

protected ArrayList<HashMap<String,String>> doInBackground(String... args) {
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
ArrayList<HashMap<String,String>> data = new ArrayList<HashMap<String, String>>();
String jsonStr = sh.makeServiceCall(INTEREST_ACCEPT_URL, ServiceHandler.GET);

Log.d("Response: ", "> " + jsonStr);

if (jsonStr != null) {
try {
JSONArray jsonary = new JSONArray(jsonStr);

System.out.println("Test jsonObj"+jsonary);


for (int i = 0; i < jsonary.length(); i++) {
c = jsonary.getJSONObject(i);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(INTERESTACCEPT_USER_NAME, c.getString(INTERESTACCEPT_USER_NAME));
map.put(INTEREST_ACCEPT_PRICE,c.getString(INTEREST_ACCEPT_PRICE));
map.put(INTEREST_ACCEPT_DISCOUNT, c.getString(INTEREST_ACCEPT_DISCOUNT));
map.put(INTEREST_ACCEPT_PRODUCTID, c.getString(INTEREST_ACCEPT_PRODUCTID));
map.put(INTEREST_ACCEPT_IMAGEURL, c.getString(INTEREST_ACCEPT_IMAGEURL));
//map.put(INTEREST_ACCEPT_AGE, c.getString(INTEREST_ACCEPT_AGE)+" years");
//map.put(INTEREST_ACCEPT_LOCATION, c.getString(INTEREST_ACCEPT_LOCATION));
// adding HashList to ArrayList

JSONArray colors=c.getJSONArray(INTEREST_ACCEPT_COLOR);
JSONArray sizes=c.getJSONArray(INTEREST_ACCEPT_SIZES);

user_img=c.getString(INTEREST_ACCEPT_COLOR);



user_img = "";
userImgArrayList = new ArrayList<String>();
JSONArray picarray = c.getJSONArray(INTEREST_ACCEPT_COLOR);
for(int a=0;a< picarray.length();a++)
{
user_img = picarray.getString(a);
userImgArrayList.add(user_img);
}
Log.d("mylog", "curent color = " + userImgArrayList);

if(userImgArrayList.size()==0)
{
Log.e("Size zero","No set color here");

}
else if(userImgArrayList.size()==1)
{
first=userImgArrayList.get(0);
second="#ffffff";
third="#ffffff";

}
else if(userImgArrayList.size()==2)
{
first=userImgArrayList.get(0);
second=userImgArrayList.get(1);
third="#ffffff";

}
else if(userImgArrayList.size()==3)
{
first=userImgArrayList.get(0);
second=userImgArrayList.get(1);
third=userImgArrayList.get(2);

}
System.out.println("Color First"+first);
System.out.println("Color Second"+second);
System.out.println("Color Third"+third);
System.out.println("Color Fourth"+fourth);



data.add(map);
}


} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return data;
}


MyAdapter.java

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.product_listing_items, null);
holder.propic = (ImageView) convertView.findViewById(R.id.productlist_img);
holder.txtproname = (TextView) convertView.findViewById(R.id.productlist_name);
holder.txtprice = (TextView) convertView.findViewById(R.id.productlist_price);
holder.firstcolor = (TextView) convertView.findViewById(R.id.firstcolor);


convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.txtproname.setText(listData.get(position).get(TAG_NAME));
holder.txtprice.setText(listData.get(position).get(TAG_PRICE));

int firstcolor=0;
firstcolor=Integer.parseInt(first);



holder.firstcolor.setBackgroundColor((firstcolor));

Answer

you need to use it in this way(if it is hexcode like #000000, #ffffff)

holder.firstcolor.setBackgroundColor(Color.parseColor(first));

argument for parseColor will accept String , so please carefull for that