如何存放多个JSON阵列的项目,显示它的ListView? [英] How to store items of multiple JSON Array and display it listview?

查看:264
本文介绍了如何存放多个JSON阵列的项目,显示它的ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我使用的ListView,我也做的JSON解析。

In my application I am using listview and I am also doing json parsing.

现在的问题是,我想在我的观点来设置颜色,颜色code我从服务器获取。

Now the issue is I want to set colors in my views, the color code I am getting from server.

以下是我的JSON响应和java code任何一个可以帮助我,在我的意见,设置颜色codeS?

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

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();

        }

    }

}

列表项

 <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>

在这里输入的形象描述

推荐答案

您可以设置列表视图行颜色按下面的JSON响应:

You can set listview' row color as per your JSON response below :

@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{

               **Add you code here for color.**       

                view.setBackgroundColor(listData.get(position).get(INTEREST_ACCEPT_COLOR));
                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;
        }

编辑:

有关展示给你的TextView的颜色,你应该首先把所有的颜色HashMap和HashMap的是传递给您的适配器。

For showing colors to your TextView , you should first put all the colors in Hashmap and pass that hashmap to your adapter.

现在键可以设置颜色为你的TextView的基础上。

Now on the basis of keys you can set colors to your Textview.

HashMap<String, String> hashmap;

传递的Hashmap的ArrayList中的方法。

Method for passing Hashmap in ArrayList.

public ArrayList<HashMap<String, String>> getAllColor(String firstcolor , String second color , String thirdcolor)
    {
        ArrayList<HashMap<String, String>> array_list = new ArrayList<HashMap<String, String>>();


            hashmap = new HashMap<String, String>();
            hashmap.put("firstcolor", firstcolor);
            hashmap.put("second color",secondcolor);
            hashmap.put("thirdcolor",thirdcolor);

            array_list.add(hashmap);

        }
        return array_list;
    }

现在呼吁通过你的颜色此方法。并通过您的ArrayList到您的适配器。。和getView()方法,你可以像下面的键检索:

Now Call this method for passing your colors. And pass your Arraylist to your ADAPTER .. And In getView() method you can retrieve it by keys like below :

**的ArrayList&LT;&HashMap的LT;字符串,字符串&GT;&GT; ArrayList的** //声明全局

的ArrayList =你的adapterList; //声明适配器的构造

arrayList = "Your adapterList"; // declare in adapter's constructor

在getView():

in getView() :

if (arrayList.size() != 0)
            {
                for (int i = 0; i < arrayList.size(); i++)
                {
                    textView1.setBackgroundColor(arrayList.get(i).get("firstcolor"));
                    textView2.setBackgroundColor(arrayList.get(i).get("secondcolor"));
                    textView3.setBackgroundColor(arrayList.get(i).get("thirdcolor"));

                }
             }

希望这个时候就会解决你的问题。

Hope this time it will solve your problem.

这篇关于如何存放多个JSON阵列的项目,显示它的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆