how to pass data from recyclerView to adapter for dynamic layout
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 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
add a comment |
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 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
Make it global and pass it to adapter constructor.
– Piyush
Nov 12 '18 at 11:14
how can you suggext me this
– Anser Abbas
Nov 12 '18 at 11:15
Create another variabletype
inside your products object class and according to object type set your dynamic layout.
– Kunu
Nov 12 '18 at 11:15
not working fine
– Anser Abbas
Nov 12 '18 at 11:29
add a comment |
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 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
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 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
android android-studio android-fragments android-recyclerview recycler-adapter
edited Nov 12 '18 at 12:07
Rutvik Bhatt
1,094415
1,094415
asked Nov 12 '18 at 11:11
Anser AbbasAnser Abbas
48
48
Make it global and pass it to adapter constructor.
– Piyush
Nov 12 '18 at 11:14
how can you suggext me this
– Anser Abbas
Nov 12 '18 at 11:15
Create another variabletype
inside your products object class and according to object type set your dynamic layout.
– Kunu
Nov 12 '18 at 11:15
not working fine
– Anser Abbas
Nov 12 '18 at 11:29
add a comment |
Make it global and pass it to adapter constructor.
– Piyush
Nov 12 '18 at 11:14
how can you suggext me this
– Anser Abbas
Nov 12 '18 at 11:15
Create another variabletype
inside your products object class and according to object type set your dynamic layout.
– Kunu
Nov 12 '18 at 11:15
not working fine
– Anser Abbas
Nov 12 '18 at 11:29
Make it global and pass it to adapter constructor.
– Piyush
Nov 12 '18 at 11:14
Make it global and pass it to adapter constructor.
– Piyush
Nov 12 '18 at 11:14
how can you suggext me this
– Anser Abbas
Nov 12 '18 at 11:15
how can you suggext me this
– Anser Abbas
Nov 12 '18 at 11:15
Create another variable
type
inside your products object class and according to object type set your dynamic layout.– Kunu
Nov 12 '18 at 11:15
Create another variable
type
inside your products object class and according to object type set your dynamic layout.– Kunu
Nov 12 '18 at 11:15
not working fine
– Anser Abbas
Nov 12 '18 at 11:29
not working fine
– Anser Abbas
Nov 12 '18 at 11:29
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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;
}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 12 '18 at 11:53
honey_ramgarhiahoney_ramgarhia
1167
1167
add a comment |
add a comment |
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
answered Nov 12 '18 at 11:58
Sultan MahmudSultan Mahmud
23017
23017
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Make it global and pass it to adapter constructor.
– Piyush
Nov 12 '18 at 11:14
how can you suggext me this
– Anser Abbas
Nov 12 '18 at 11:15
Create another variable
type
inside your products object class and according to object type set your dynamic layout.– Kunu
Nov 12 '18 at 11:15
not working fine
– Anser Abbas
Nov 12 '18 at 11:29