how to pass data from recyclerView to adapter for dynamic layout










-1














I am making dynamic recyclerView with multiple layouts. I have made three to four layout which should be changed dynamically according to the style type id. Now I am getting style Id in recyclerview and how I can pass this value to the adapter.



here is my snapshot
here is my snapshot



here is my code form which i am getting style Type Id:



int StyleTypeId = object.getInt("StyleTypeID");


here is my adapter code:



@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View view = null;
if (viewType == 1)
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_electronic_category, parent, false);
else if (viewType == 2)
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_latest_product, parent, false);

return new ViewHolder(view);


@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position)
if (holder.getItemViewType() == 1)
holder.setData(products.get(position));
else if (holder.getItemViewType() == 2)
holder.setLatestProductData(products.get(position));


@Override
public int getItemViewType(int position)
return 2;



my problem is that how I can pass the style type Id from JSON object to adapter runtime










share|improve this question























  • Make it global and pass it to adapter constructor.
    – Piyush
    Nov 12 at 11:14










  • how can you suggext me this
    – Anser Abbas
    Nov 12 at 11:15










  • Create another variable type inside your products object class and according to object type set your dynamic layout.
    – Kunu
    Nov 12 at 11:15










  • not working fine
    – Anser Abbas
    Nov 12 at 11:29















-1














I am making dynamic recyclerView with multiple layouts. I have made three to four layout which should be changed dynamically according to the style type id. Now I am getting style Id in recyclerview and how I can pass this value to the adapter.



here is my snapshot
here is my snapshot



here is my code form which i am getting style Type Id:



int StyleTypeId = object.getInt("StyleTypeID");


here is my adapter code:



@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View view = null;
if (viewType == 1)
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_electronic_category, parent, false);
else if (viewType == 2)
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_latest_product, parent, false);

return new ViewHolder(view);


@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position)
if (holder.getItemViewType() == 1)
holder.setData(products.get(position));
else if (holder.getItemViewType() == 2)
holder.setLatestProductData(products.get(position));


@Override
public int getItemViewType(int position)
return 2;



my problem is that how I can pass the style type Id from JSON object to adapter runtime










share|improve this question























  • Make it global and pass it to adapter constructor.
    – Piyush
    Nov 12 at 11:14










  • how can you suggext me this
    – Anser Abbas
    Nov 12 at 11:15










  • Create another variable type inside your products object class and according to object type set your dynamic layout.
    – Kunu
    Nov 12 at 11:15










  • not working fine
    – Anser Abbas
    Nov 12 at 11:29













-1












-1








-1







I am making dynamic recyclerView with multiple layouts. I have made three to four layout which should be changed dynamically according to the style type id. Now I am getting style Id in recyclerview and how I can pass this value to the adapter.



here is my snapshot
here is my snapshot



here is my code form which i am getting style Type Id:



int StyleTypeId = object.getInt("StyleTypeID");


here is my adapter code:



@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View view = null;
if (viewType == 1)
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_electronic_category, parent, false);
else if (viewType == 2)
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_latest_product, parent, false);

return new ViewHolder(view);


@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position)
if (holder.getItemViewType() == 1)
holder.setData(products.get(position));
else if (holder.getItemViewType() == 2)
holder.setLatestProductData(products.get(position));


@Override
public int getItemViewType(int position)
return 2;



my problem is that how I can pass the style type Id from JSON object to adapter runtime










share|improve this question















I am making dynamic recyclerView with multiple layouts. I have made three to four layout which should be changed dynamically according to the style type id. Now I am getting style Id in recyclerview and how I can pass this value to the adapter.



here is my snapshot
here is my snapshot



here is my code form which i am getting style Type Id:



int StyleTypeId = object.getInt("StyleTypeID");


here is my adapter code:



@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View view = null;
if (viewType == 1)
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_electronic_category, parent, false);
else if (viewType == 2)
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_latest_product, parent, false);

return new ViewHolder(view);


@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position)
if (holder.getItemViewType() == 1)
holder.setData(products.get(position));
else if (holder.getItemViewType() == 2)
holder.setLatestProductData(products.get(position));


@Override
public int getItemViewType(int position)
return 2;



my problem is that how I can pass the style type Id from JSON object to adapter runtime







android android-studio android-fragments android-recyclerview recycler-adapter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 12:07









Rutvik Bhatt

1,052414




1,052414










asked Nov 12 at 11:11









Anser Abbas

48




48











  • Make it global and pass it to adapter constructor.
    – Piyush
    Nov 12 at 11:14










  • how can you suggext me this
    – Anser Abbas
    Nov 12 at 11:15










  • Create another variable type inside your products object class and according to object type set your dynamic layout.
    – Kunu
    Nov 12 at 11:15










  • not working fine
    – Anser Abbas
    Nov 12 at 11:29
















  • Make it global and pass it to adapter constructor.
    – Piyush
    Nov 12 at 11:14










  • how can you suggext me this
    – Anser Abbas
    Nov 12 at 11:15










  • Create another variable type inside your products object class and according to object type set your dynamic layout.
    – Kunu
    Nov 12 at 11:15










  • not working fine
    – Anser Abbas
    Nov 12 at 11:29















Make it global and pass it to adapter constructor.
– Piyush
Nov 12 at 11:14




Make it global and pass it to adapter constructor.
– Piyush
Nov 12 at 11:14












how can you suggext me this
– Anser Abbas
Nov 12 at 11:15




how can you suggext me this
– Anser Abbas
Nov 12 at 11:15












Create another variable type inside your products object class and according to object type set your dynamic layout.
– Kunu
Nov 12 at 11:15




Create another variable type inside your products object class and according to object type set your dynamic layout.
– Kunu
Nov 12 at 11:15












not working fine
– Anser Abbas
Nov 12 at 11:29




not working fine
– Anser Abbas
Nov 12 at 11:29












2 Answers
2






active

oldest

votes


















0














create your adapter like this



int StyleTypeId = object.getInt("StyleTypeID");
YourAdapter adapter = new YourAdapter(StyleTypeId);


then inside your adapter



class YourAdapter extends RecyclerView.Adapter<YourViewHolder> 
private int styledId;
YourAdapter(int styleId) this.styleId = styleId;



and in onCreateViewHolder() you can use the styleId to create appropriate view.






share|improve this answer




























    0














    int StyleTypeId = object.getInt("StyleTypeID");
    X adapter = new X(StyleTypeId);


    then your adapter class add the following snippet:



    private int viewType;
    X(int styleId) this.viewType = styleId;


    In the getItemViewType method return your viewtype instead of static variable



     @Override
    public int getItemViewType(int position)
    return viewType;






    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53260933%2fhow-to-pass-data-from-recyclerview-to-adapter-for-dynamic-layout%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      create your adapter like this



      int StyleTypeId = object.getInt("StyleTypeID");
      YourAdapter adapter = new YourAdapter(StyleTypeId);


      then inside your adapter



      class YourAdapter extends RecyclerView.Adapter<YourViewHolder> 
      private int styledId;
      YourAdapter(int styleId) this.styleId = styleId;



      and in onCreateViewHolder() you can use the styleId to create appropriate view.






      share|improve this answer

























        0














        create your adapter like this



        int StyleTypeId = object.getInt("StyleTypeID");
        YourAdapter adapter = new YourAdapter(StyleTypeId);


        then inside your adapter



        class YourAdapter extends RecyclerView.Adapter<YourViewHolder> 
        private int styledId;
        YourAdapter(int styleId) this.styleId = styleId;



        and in onCreateViewHolder() you can use the styleId to create appropriate view.






        share|improve this answer























          0












          0








          0






          create your adapter like this



          int StyleTypeId = object.getInt("StyleTypeID");
          YourAdapter adapter = new YourAdapter(StyleTypeId);


          then inside your adapter



          class YourAdapter extends RecyclerView.Adapter<YourViewHolder> 
          private int styledId;
          YourAdapter(int styleId) this.styleId = styleId;



          and in onCreateViewHolder() you can use the styleId to create appropriate view.






          share|improve this answer












          create your adapter like this



          int StyleTypeId = object.getInt("StyleTypeID");
          YourAdapter adapter = new YourAdapter(StyleTypeId);


          then inside your adapter



          class YourAdapter extends RecyclerView.Adapter<YourViewHolder> 
          private int styledId;
          YourAdapter(int styleId) this.styleId = styleId;



          and in onCreateViewHolder() you can use the styleId to create appropriate view.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 11:53









          honey_ramgarhia

          1167




          1167























              0














              int StyleTypeId = object.getInt("StyleTypeID");
              X adapter = new X(StyleTypeId);


              then your adapter class add the following snippet:



              private int viewType;
              X(int styleId) this.viewType = styleId;


              In the getItemViewType method return your viewtype instead of static variable



               @Override
              public int getItemViewType(int position)
              return viewType;






              share|improve this answer

























                0














                int StyleTypeId = object.getInt("StyleTypeID");
                X adapter = new X(StyleTypeId);


                then your adapter class add the following snippet:



                private int viewType;
                X(int styleId) this.viewType = styleId;


                In the getItemViewType method return your viewtype instead of static variable



                 @Override
                public int getItemViewType(int position)
                return viewType;






                share|improve this answer























                  0












                  0








                  0






                  int StyleTypeId = object.getInt("StyleTypeID");
                  X adapter = new X(StyleTypeId);


                  then your adapter class add the following snippet:



                  private int viewType;
                  X(int styleId) this.viewType = styleId;


                  In the getItemViewType method return your viewtype instead of static variable



                   @Override
                  public int getItemViewType(int position)
                  return viewType;






                  share|improve this answer












                  int StyleTypeId = object.getInt("StyleTypeID");
                  X adapter = new X(StyleTypeId);


                  then your adapter class add the following snippet:



                  private int viewType;
                  X(int styleId) this.viewType = styleId;


                  In the getItemViewType method return your viewtype instead of static variable



                   @Override
                  public int getItemViewType(int position)
                  return viewType;







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 at 11:58









                  Sultan Mahmud

                  23017




                  23017



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53260933%2fhow-to-pass-data-from-recyclerview-to-adapter-for-dynamic-layout%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3

                      Museum of Modern and Contemporary Art of Trento and Rovereto