4

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

        }
    }

}
Sun
  • 6,768
  • 25
  • 76
  • 131
  • Pass category name or category Id in `ViewPagerAdapter(...)` as a `Argument` and filter it – M D May 05 '15 at 08:39
  • @MD I have tried did not get solution, can you point out my mistake ? – Sun May 05 '15 at 08:53
  • Where you passed category to `ViewPagerAdapter`? – M D May 05 '15 at 08:54
  • I posted below EDITED, i guess i am doing it in a wrong way – Sun May 05 '15 at 08:55
  • yeah i did same, wait let me post my updated ViewPagerAdapter code – Sun May 05 '15 at 08:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76970/discussion-between-m-d-and-sun). – M D May 05 '15 at 08:58
  • use this `((ViewPager) container).addView(itemView,0);` instead of this `((ViewPager) container).addView(itemView);` – Amrut Bidri May 05 '15 at 09:04
  • In this function `getItemsArrayList()` add a one string parameter which will be `categoryID`. Like `getItemsArrayList(String catID)`. Now when you call this `menus.getItemsArrayList()` then get catID on item click and pass it to that method. – Piyush May 05 '15 at 11:01
  • Where is the parsing part of JSON String? We can't know the variables values without seeing them. It seems like you put all the sublist items to one menu – mgokgoz May 05 '15 at 11:20
  • @mgokgoz i have posted JSON Parsing code as well, please check : MenusActivity.java – Sun May 05 '15 at 11:24
  • 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 – mgokgoz May 05 '15 at 11:32
  • so how can i resolve this issue ? please show me the changes and post your solution as your answer, so i will be able to accept that @mgokgoz – Sun May 05 '15 at 11:36

1 Answers1

3

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);
                }
mgokgoz
  • 186
  • 1
  • 9