In my application I am using listview and I am also doing json parsing.
Now the issue is I want to set colors in my views, the color code I am getting from server.
Following is my json response and java code can any one help me, to set color codes in my views?
[
{
"id_product": "1445",
"name": "Stylish Sleeveless Leather Vest",
"price": 1990,
"discount": 199,
"colors": [
"#000000",
"#7E3517",
"#C85A17"
],
"sizes": [
"Medium",
"Large",
"Small"
],
"img_url": "",
"popup_images": [
]
},
{
"id_product": "1427",
"name": "Stylish Slim Fit Designed PU Leather Jacket",
"price": 3290,
"discount": 329,
"colors": [
"#000000",
"#C85A17"
],
"sizes": [
"Large",
"Medium",
"Small"
],
"img_url": "",
"popup_images": [
]
}
]
MainActivity.java
public class Product_Listing extends Fragment{
private ListView listview;
private ArrayList<HashMap<String,String>> aList;
private static String INTEREST_ACCEPT_URL = "";
// private static final String INTEREST_ACCEPT="interestaccept";
private static final String INTERESTACCEPT_USER_NAME="name";
private static final String INTEREST_ACCEPT_PRICE="price";
private static final String INTEREST_ACCEPT_PRODUCTID="id_product";
private static final String INTEREST_ACCEPT_DISCOUNT="discount";
private static final String INTEREST_ACCEPT_IMAGEURL="img_url";
private static final String INTEREST_ACCEPT_COLOR="colors";
private static final String INTEREST_ACCEPT_SIZES="sizes";
private CustomAdapterAccept adapter;
private String brandnms;
String user_img;
ArrayList<String> userImgArrayList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.product_listing_listivew, container, false);
Bundle bundle = this.getArguments();
brandnms = bundle.getString("Brandkeyword");
listview=(ListView)rootView.findViewById(R.id.listview_productlistings);
INTEREST_ACCEPT_URL = "";
new LoadAlbums().execute();
return rootView;
}
public class CustomAdapterAccept extends BaseAdapter{
private Context context;
private ArrayList<HashMap<String,String>> listData;
private AQuery aQuery;
String rup="\u20B9";
private static final String TAG_NAME="name";
private static final String TAG_IMAGE="img_url";
private static final String TAG_PRICE="price";
public CustomAdapterAccept(Context context,ArrayList<HashMap<String,String>> listData) {
this.context = context;
this.listData=listData;
aQuery = new AQuery(this.context);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@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.txtpr = (TextView) convertView.findViewById(R.id.productlist_name);
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));
aQuery.id(holder.propic).image(listData.get(position).get(TAG_IMAGE),true,true,0,R.drawable.meracaslogo);
// image parameter : 1 : memory cache,2:file cache,3:target width,4:fallback image
return convertView;
}
class ViewHolder{
TextView txtprice;
ImageView propic;
TextView txtproname;
}
}
class LoadAlbums extends AsyncTask<String, String, ArrayList<HashMap<String,String>>> {
private ProgressDialog pDialog;
private String first;
private String sizefirst;
private JSONObject c;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(true);
// pDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.custom_progress));
pDialog.setCancelable(false);
pDialog.show();
}
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);
// Getting JSON Array node
// interestaccept = jsonObj.getJSONArray(INTEREST_ACCEPT);
for (int i = 0; i < jsonary.length(); i++) {
c = jsonary.getJSONObject(i);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
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=jsonObj.getString(USER_IMG);
user_img = "";
userImgArrayList = new ArrayList<String>();//declare userImgArrayList globally like ArrayList<String> userImgArrayList;
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 pro pic = " + userImgArrayList);
first=userImgArrayList.get(i);
System.out.println("Color First"+first);
//first=colors.getJSONObject(a);
/* for(int j=0;j<colors.length();j++)
{
//first=colors.getJSONObject(j);
}
for(int s=0;s<sizes.length();s++)
{
sizefirst=sizes.getString(s);
}
System.out.println("Color First"+first);
System.out.println("Colors"+colors);*/
data.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return data;
}
protected void onPostExecute(ArrayList<HashMap<String,String>> result) {
super.onPostExecute(result);
// dismiss the dialog after getting all albums
if (pDialog.isShowing())
pDialog.dismiss();
// updating UI from Background Thread
aList = new ArrayList<HashMap<String, String>>();
aList.addAll(result);
adapter = new CustomAdapterAccept(getActivity(),result);
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}
listitem
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Colors:" />
<TextView
android:id="@+id/firstcolor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="7dp"
/>
<TextView
android:id="@+id/secondcolor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
/>
<TextView
android:id="@+id/thirdcolor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
/>
<TextView
android:id="@+id/fourthcolor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
/>
</LinearLayout>
