如何使用JSON对象传递JSON数组 [英] How to Pass JSON Array using JSON Object

查看:153
本文介绍了如何使用JSON对象传递JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GridView控件包含类别,当我做水龙头上的任何GridView的项目(即 - 任何类别)。我想证明这些物品属于只在ViewPager特定类别

例如,我有两个类别第一 - 电子和第二 - 家电,仍然每当我做水龙头在家电类别 - 它显示所有项目(从电子1到电器2 ),而它显示唯一项目那些下的家电类别

问题

仍然显示在ViewPager的所有项目,无论哪一类已被用户窃听.... 如何可以我证明这些物品属于窃听类别只?

 公共类MainActivity延伸活动{    ArrayList的<项目> itemsArrayList;
    ...    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        的setContentView(R.layout.activity_main);        itemsArrayList =新的ArrayList<项目>();        ...        gridView.setOnItemClickListener(新OnItemClickListener(){            @覆盖
            公共无效onItemClick(适配器视图<>为arg0,ARG1查看,INT位置,
                    长ID){                。字符串categoryName = menusArrayList.get(位置).getName()的toString();
                Log.d(类别名称,类别名称);                菜单= menusArrayList.get(位置);
                itemsArrayList = menus.getItemsArrayList();                适配器=新ViewPagerAdapter(MenusActivity.this,R.layout.adapter_viewpager,itemsArrayList);                viewPager.setAdapter(适配器);
            }
        });
    }
    类JSONAsyncTask扩展的AsyncTask<弦乐,太虚,布尔> {        ProgressDialog对话框;        @覆盖
        在preExecute保护无效(){
            super.on preExecute();        }        @覆盖
        保护布尔doInBackground(字符串的URL ...){
            尝试{                .....                    JSONArray jsonArray = jsonObject.getJSONArray(类);                    的for(int i = 0; I< jsonArray.length();我++){
                        JSONObject的jObject = jsonArray.getJSONObject(I)                        菜单=新菜单();                        menus.setName(jObject.getString(名字));
                        menus.setImage(jObject.getString(图像));                        JSONArray imagesArray = jObject.getJSONArray(项目);                        对于(INT J = 0; J< imagesArray.length(); J ++)
                        {
                            JSONObject的imagesObject = imagesArray.getJSONObject(J);                            项目=新项目();
                            items.setTitle(imagesObject.getString(标题));
                            items.setImage(imagesObject.getString(图像));                            itemsArrayList.add(项目);
                        }                        menus.setItemsArrayList(itemsArrayList);
                        menusArrayList.add(菜单);
                    }
                    返回true;
                }
                ...
            返回false;
        }        保护无效onPostExecute(布尔结果){
            dialog.cancel();
            ...        }
    }}


解决方案

您itemArrayList是全球性的,并在解析你每次你读一个类别时将项目添加到该列表。出于这个原因,你itemListArray包含的所有项目。既然是参考列表时,您将项目添加到列表中,你也增加项目previous类,因为它们引用相同的列表。试着改变你的内循环是这样的。

 的ArrayList<项目> itemsArrayList =新的ArrayList<项目>();
对于(INT J = 0; J< imagesArray.length(); J ++)
                        {
                        JSONObject的imagesObject = imagesArray.getJSONObject(J);                    项目=新项目();
                    items.setTitle(imagesObject.getString(标题));
                    items.setImage(imagesObject.getString(图像));                    itemsArrayList.add(项目);
                }

GridView contains Categories, and when I do tap on any of the GridView item (i.e - any Category) i would like to show items those belongs to that particular Category only in ViewPager.

For example, I have two categories first - Electronics and second - Appliances, still whenever i do tap on Appliances category - it showing all the items (from Electronics 1 to Appliances 2) whereas it has to show only items those are under Appliances category

ISSUE

Still showing all the items in a ViewPager, no matter which category has been tapped by user.... How may i show those Items that belongs to tapped Category only ?

public class MainActivity extends Activity {

    ArrayList<Items> itemsArrayList;
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        itemsArrayList = new ArrayList<Items>();

        ...

        gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long id) {

                String categoryName = menusArrayList.get(position).getName().toString();
                Log.d("categoryName:", categoryName);                   

                menus = menusArrayList.get(position);                               
                itemsArrayList = menus.getItemsArrayList();     

                adapter = new ViewPagerAdapter(MenusActivity.this, R.layout.adapter_viewpager, itemsArrayList);

                viewPager.setAdapter(adapter);
            }
        });
    }


    class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {

        ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected Boolean doInBackground(String... urls) {
            try {

                .....

                    JSONArray jsonArray = jsonObject.getJSONArray("category");

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jObject = jsonArray.getJSONObject(i);

                        menus = new Menus();

                        menus.setName(jObject.getString("name"));                       
                        menus.setImage(jObject.getString("image"));

                        JSONArray imagesArray = jObject.getJSONArray("items");                      

                        for(int j=0; j<imagesArray.length(); j++)
                        {
                            JSONObject imagesObject = imagesArray.getJSONObject(j);

                            items = new Items();
                            items.setTitle(imagesObject.getString("title"));
                            items.setImage(imagesObject.getString("image"));                                

                            itemsArrayList.add(items);
                        }       

                        menus.setItemsArrayList(itemsArrayList);                        
                        menusArrayList.add(menus);
                    }
                    return true;
                }
                ...
            return false;
        }

        protected void onPostExecute(Boolean result) {
            dialog.cancel();
            ...

        }
    }

}

解决方案

Your itemArrayList is global and on parsing you're adding items to that list each time you read a category. For that reason your itemListArray include all items. Since it is reference to a list when you add an item to list, you're also adding item to previous categories since they reference to same list. Try changing your inner loop like this.

ArrayList<Items> itemsArrayList = new ArrayList<Items>();    
for(int j=0; j<imagesArray.length(); j++)
                        {
                        JSONObject imagesObject = imagesArray.getJSONObject(j);

                    items = new Items();
                    items.setTitle(imagesObject.getString("title"));
                    items.setImage(imagesObject.getString("image"));                                

                    itemsArrayList.add(items);
                }

这篇关于如何使用JSON对象传递JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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