我從我的產品列表中獲取服務器的顏色代碼,顏色代碼越來越完美,但我試圖將其設置在我的textview
但無法設置color
,以下是我的代碼段代碼和adapter
,任何幫助將不勝感激。如何使用顏色代碼使用JSON
顏色代碼,我從服務器獲取
[
{
"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));
這取決於你的顏色是存儲在您'JSONArray' ...是十六進制RGB的方式嗎? –
我的顏色代碼是這種格式的#000000 –
@BartoszLipinski參見我編輯的問題 –