在我的应用程序中,我使用listview
,同时也在进行json
解析。,根据颜色代码,我得到了它,并将其设置为ShapeDrawable,但问题是我在每个列表项中都得到了最后一个颜色代码数组的值,下面是我的json响应和代码,有人能帮我吗?
我的Json响应
[
{
"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": [
]
}
]
现在的问题是,我的每个列表项中只有两种颜色,而后面的两种颜色
"colors": [
"#000000",
"#C85A17"
],
我的异步任务
class LoadAlbums extends AsyncTask<String, String, ArrayList<HashMap<String,String>>> {
private ProgressDialog pDialog;
private JSONObject c;
private String myColor;
@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);
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));
// JSONArray colors=c.getJSONArray(INTEREST_ACCEPT_COLOR);
// JSONArray sizes=c.getJSONArray(INTEREST_ACCEPT_SIZES);
//user_img=c.getString(INTEREST_ACCEPT_COLOR);
multimage=c.getString(INTEREST_ACCEPT_IMAGEARRAY);
multimage = "";
userImgsArrayList = new ArrayList<String>();
JSONArray picsarray = c.getJSONArray(INTEREST_ACCEPT_IMAGEARRAY);
for(int a=0;a< picsarray.length();a++)
{
multimage = picsarray.getString(a);
userImgsArrayList.add(multimage);
}
Log.d("mylog", "userimagesarraylist = " + userImgsArrayList);
user_img = "";
userImgArrayList = new ArrayList<String>();
JSONArray picarray = c.getJSONArray(INTEREST_ACCEPT_COLOR);
map.put("colors", picarray.toString());
/*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;
}
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();
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
}
});
}
}
我的适配器
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.firstcolor = (TextView) convertView.findViewById(R.id.firstcolor);
holder.secondcolor = (TextView) convertView.findViewById(R.id.secondcolor);
holder.thirdcolor = (TextView) convertView.findViewById(R.id.thirdcolor);
holder.fourthcolor = (TextView) convertView.findViewById(R.id.fourthcolor);
holder.cartview=(ImageView)convertView.findViewById(R.id.cartviews);
holder.addtocart=(Button)convertView.findViewById(R.id.productlist_addtocart);
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));
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(listData.get(position).get("colors"));
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for(int a=0;a< jsonArray.length();a++)
{
try {
user_img = jsonArray.getString(a);
userImgArrayList.add(user_img);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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);
ShapeDrawable biggerCircle= new ShapeDrawable( new OvalShape());
biggerCircle.setIntrinsicHeight( 60 );
biggerCircle.setIntrinsicWidth( 60);
biggerCircle.setBounds(new Rect(30, 30, 30, 30));
biggerCircle.getPaint().setColor(Color.parseColor(first));
holder.firstcolor.setBackgroundDrawable(biggerCircle);
ShapeDrawable biggerCirclesec= new ShapeDrawable( new OvalShape());
biggerCirclesec.setIntrinsicHeight( 60 );
biggerCirclesec.setIntrinsicWidth( 60);
biggerCirclesec.setBounds(new Rect(30, 30, 30, 30));
biggerCirclesec.getPaint().setColor(Color.parseColor(second));
holder.secondcolor.setBackgroundDrawable(biggerCirclesec);
ShapeDrawable biggerCirclethree= new ShapeDrawable( new OvalShape());
biggerCirclethree.setIntrinsicHeight( 60 );
biggerCirclethree.setIntrinsicWidth( 60);
biggerCirclethree.setBounds(new Rect(30, 30, 30, 30));
biggerCirclethree.getPaint().setColor(Color.parseColor(third));
holder.thirdcolor.setBackgroundDrawable(biggerCirclethree);
aQuery.id(holder.propic).image(listData.get(position).get(TAG_IMAGE),true,true,0,R.drawable.meracaslogo);
holder.cartview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AddtoCart tf = new AddtoCart();
Bundle bundle = new Bundle();
// bundle.putString("Brandnunamm", aList.get(position).get((USER_NAME)));
tf.setArguments(bundle);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frame_container, tf);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}
});
holder.addtocart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new AttemptLogin().execute();
//Toast.makeText(getActivity(), "Added to cart", Toast.LENGTH_SHORT).show();
}
});
holder.propic.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(userImgsArrayList != null && userImgsArrayList.size() > 0) {
Fullimages tf = new Fullimages();
Bundle bundle = new Bundle();
bundle.putStringArrayList("user_images", userImgsArrayList);
// bundle.putString("Brandnunamm", aList.get(position).get((USER_NAME)));
tf.setArguments(bundle);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frame_container, tf);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}
else
{
Toast.makeText(getActivity(), "No Images Availabel", Toast.LENGTH_SHORT).show();
}
}
});
// image parameter : 1 : memory cache,2:file cache,3:target width,4:fallback image
return convertView;
}
class ViewHolder{
public TextView fourthcolor;
public TextView thirdcolor;
public TextView secondcolor;
public ImageView cartview;
TextView txtprice;
ImageView propic;
TextView txtproname;
TextView firstcolor;
Button addtocart;
}
}
我正在获取我的每个列表项中最后一个颜色代码数组的值
因为userImgsArrayList
每次都在for循环内部创建,用于迭代jsonary
JSONArray。
不在ArrayList
中存储字符串颜色,只将其存储在用于存储其他详细信息的Map
中:
JSONArray picColors = c.getJSONArray(INTEREST_ACCEPT_COLOR);
map.put("colors", picColors.toString());
现在在getView
方法中移动所有颜色逻辑:
JSONArray jsonArray=new JSONArray(listData.get(position).get("colors"));
for(int a=0;a< picarray.length();a++)
{
user_img = picarray.getString(a);
userImgArrayList.add(user_img);
}
////....