I want to pass data RC_1 to RC_2. How can I do this(cat, info, orderid) RecyclerView to RecyclerView?Examp.pic_1
My firebase node is like as to picture. My firebase node
//RC_1 Struct
public class Name_Struct {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//RC_2 Struct
public class Detail_Struct {
private String cat;
private String info;
private String orderid;
public String getCat() {
return cat;
}
public void setCat(String cat) {
this.cat = cat;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getOrderid() {
return orderid;
}
public void setOrderid(String orderid) {
this.orderid = orderid;
}
RC_1 for; //Myactivity_1
private void rc_1_get_name(){
Query query = FirebaseDatabase.getInstance().getReference()
.child(“Pro_”);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
mName.clear();
for(DataSnapshot snp : snapshot.getChildren()){
Name_Struct names = snp.getValue(Name_Struct.class);
for (DataSnapshot recipeSnapshot: snp.child(“basket”).getChildren()) {
mName.add(names);
}
}
mName_adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
RC_1 for; //Myadapter_ 1
@Override public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
final Name_Struct names = mName.get(position);
holder.name_.setText(names.getName()));
holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(mContext, RC_2.class);
???
mContext.startActivity(intent);
}
}
CodePudding user response:
As I see in your screenshot, the "Pro_" node contains a name that has the value of "Mike". This means that you can map that node into an object of type Name_Struct
. And you are doing that successfully. Under the same node, there is however another node called "Basket". That node is represented by a Map object, where the keys are represented by those IDs (ID_1, ID_2, etc) and the value by Detail_Struct
objects. To be able to map the data under the "Basket" node, you have to add a new property in your Name_Struct
class called basket
like this:
public class Name_Struct implements Serializable {
private String name;
private Map<String, Object> basket;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@PropertyName("Basket")
public Map<String, Object> getBasket() {
return basket;
}
public void setBasket(Map<String, Object> basket) {
this.basket = basket;
}
}
Now to pass the data to the second activity, you have to add iterate the Map and create a List<Detail_Struct>
that can be added to the Intent so it can be read in the second activity.