1

facing problem while creating horizontal listview inside listview custom row. Problem is that, first row of listview is having 3 items i.e. A, B C. and second row of listview , having 6 items in horizontal list view i.e. A,B,C, D,E,F.

I am not able to get out of this problem.

enter image description here

Here is code snippets

Creating 3 arraylist

List<SectionArea> sectionAreas;
List<BannerArea> bannerAreas;
List<Wrapper> wrappers;

Code to fetch json data from server

 wrappers = new ArrayList<Wrapper>();
                            // Code to get all wrapper in Icons list
                            JSONArray wrapperArray = object.getJSONArray("wrapper");
                            for (int i = 0; i < wrapperArray.length(); i++) {

                                sectionAreas = new ArrayList<SectionArea>();
                                bannerAreas = new ArrayList<BannerArea>();

                                JSONObject wrapperObj = wrapperArray.getJSONObject(i);

                                String section_image = wrapperObj.getString("section_image");
                                String category_id = wrapperObj.getString("category_id");
                                String category_name = wrapperObj.getString("category_name");
                                String banner_title = wrapperObj.getString("banner_title");

                                JSONArray sectioArray = wrapperObj.getJSONArray("sectionarea");
                                for (int j = 0; j < sectioArray.length(); j++) {

                                    JSONObject jsonObject = sectioArray.getJSONObject(j);

                                    String product_id = jsonObject.getString("product_id");
                                    String sec_category_id = jsonObject.getString("category_id");
                                    String name = jsonObject.getString("name");
                                    String image = jsonObject.getString("image");
                                    String price = jsonObject.getString("price");
                                    String special = jsonObject.getString("special");
                                    String discount = jsonObject.getString("discount");

                                    SectionArea sectionArea = new SectionArea(product_id, sec_category_id, name, image, price, special, discount);
                                    sectionAreas.add(sectionArea);

                                }

                                JSONArray bannerAreaArray = wrapperObj.getJSONArray("bannerarea");
                                for (int k = 0; k < bannerAreaArray.length(); k++) {


                                    JSONObject jsonObject = bannerAreaArray.getJSONObject(k);
                                    String image = jsonObject.getString("image");
                                    String tag = jsonObject.getString("tag");
                                    String id = jsonObject.getString("id");

                                    BannerArea bannerArea = new BannerArea(image, tag, id);
                                    bannerAreas.add(bannerArea);
                                }

                                Wrapper wrapper = new Wrapper(section_image, category_id, category_name, banner_title,
                                        sectionAreas, bannerAreas);
                                wrappers.add(wrapper);

                            }

                        }

Adding adapter

 list.setAdapter(new HomePageAdapter(context, wrappers));

Adapter code for setting Horizontal list view

 sectionAreas = wrappers.get(position).getSectionAreas();
        viewHolder.item_list.setAdapter(new HorizontaListAdapter(mContext, sectionAreas));

Please help. Thanks in advance

8
  • 1
    what issues you are getting..?
    – Meenal
    Commented Oct 5, 2016 at 11:55
  • @MeenalSharma - Issue is that second row of listview's horizontal listview have 3+3 items in it. Means 3 from 1st row and 3 form 2nd row.
    – user6303614
    Commented Oct 5, 2016 at 11:56
  • Are you clearing the list before adding the items for second view? I prefer using two different lists for two different listviews Commented Oct 5, 2016 at 12:03
  • I am using 2 different list - sectionarea and bannerarea to merged in on wrapper list.
    – user6303614
    Commented Oct 5, 2016 at 12:08
  • Share your HomePageAdapter class.
    – Beena
    Commented Oct 5, 2016 at 12:09

2 Answers 2

1

You have to use LinearlayoutManager with RecyclerView.

LinearLayoutManager layoutManager= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
0

Replace below line from your HomePageAdapter class

   viewHolder.item_list.setAdapter(new HorizontaListAdapter(mContext, sectionAreas));

With

  viewHolder.item_list.setAdapter(new HorizontaListAdapter(mContext, wrappers.get(position).getSectionAreas()));

Also replace following lines from your code of class MainActivity

          sectionAreas = new ArrayList<SectionArea>();
          bannerAreas = new ArrayList<BannerArea>();

by

    ArrayList<SectionArea>  sectionAreas = new ArrayList<SectionArea>();
    ArrayList<BannerArea>  bannerAreas = new ArrayList<BannerArea>();

And remove public variable

List<SectionArea> sectionAreas;
List<BannerArea> bannerAreas;

NOTES :

I have checked you code and find that you have used HorizontalView to display items horizontally. So just a suggestion, Instead of using any custom class you can use Android default Horizontal RecyclerView. Which is more reliable.

EX of usage:

  RecyclerView recyclerTeams= (RecyclerView) mainView.findViewById(R.id.recyclerTeams);
  LinearLayoutManager llManager = new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false);
  recyclerTeams.setLayoutManager(llManager);
2
  • @MarkLoke : Not able to test ur app as it display one loader initially. And nothing happens. Can you please provide flow so that i can check it?
    – Beena
    Commented Oct 5, 2016 at 12:39
  • I have changed HorizontalView to RecyclerView but same error. Repeating data in rows.
    – user6303614
    Commented Oct 6, 2016 at 4:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.