我的firebase db中有两种类型的产品,如下所示。
型号:ProductTypeOne.java
package fyp.hkust.facet.model;
import java.util.ArrayList;
public class ProductTypeOne {
private String name;
private String brand;
private String desc;
private String image;
private String username;
private String uid;
private ArrayList<ArrayList<String>> color;
public Product()
{
}
public Product(String name,String brand, String desc, String image,String username,String uid,ArrayList<ArrayList<String>> color) {
this.name = name;
this.brand = brand;
this.desc = desc;
this.image = image;
this.username = username;
this.uid = uid;
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public ArrayList<ArrayList<String>> getColor() {
return color;
}
public void setColor(ArrayList<ArrayList<String>> color) {
this.color = color;
}
}
型号:ProductTypeTwo.java
package fyp.hkust.facet.model;
import java.util.ArrayList;
public class ProductTypeTwo {
private String name;
private String brand;
private String desc;
private String image;
private String username;
private String uid;
private ArrayList<String> color;
public Product()
{
}
public Product(String name,String brand, String desc, String image,String username,String uid,ArrayList<String> color) {
this.name = name;
this.brand = brand;
this.desc = desc;
this.image = image;
this.username = username;
this.uid = uid;
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public ArrayList<String> getColor() {
return color;
}
public void setColor(ArrayList<String> color) {
this.color = color;
}
}
我有一个指示器可以检测到他们的类型。如何检测其类型以选择合适的模型来获取它们?请给我一些帮助。非常感谢你。
FirebaseRecyclerAdapter<Product, ProductViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Product, ProductViewHolder>(
Product.class,
R.layout.product_row,
ProductViewHolder.class,
mDatabase
) {
@Override
protected void populateViewHolder(ProductViewHolder viewHolder, Product model, int position) {
Log.d(TAG, "loading view " + position);
final String product_id = getRef(position).getKey();
viewHolder.setTitle(model.getName());
viewHolder.setDesc(model.getDesc());
viewHolder.setImage(getApplicationContext(), model.getImage());
viewHolder.setUsername(model.getUsername());
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent productDetailIntent = new Intent();
productDetailIntent.setClass(MainActivity.this, ProductDetailActivity.class);
productDetailIntent.putExtra("product_id", product_id);
Log.d(TAG + " product_id", product_id);
startActivity(productDetailIntent);
}
});
Log.d(TAG, "finish loading view");
}
};
mProductList.setAdapter(firebaseRecyclerAdapter);
答案 0 :(得分:0)
像这样创建自定义适配器
class PersonAdapter extends FirebaseRecyclerAdapter<Person, RecyclerView.ViewHolder> {
PersonAdapter(Class<Person> modelClass, DatabaseReference ref) {
super(modelClass, 0, RecyclerView.ViewHolder.class, ref);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case Person.PersonType.TYPE_1:
View userType1 = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_person_type_1, parent, false);
return new PersonType1ViewHolder(userType1);
case Person.PersonType.TYPE_2:
View userType2 = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_person_type_2, parent, false);
return new PersonType2ViewHolder(userType2);
}
return super.onCreateViewHolder(parent, viewType);
}
@Override
protected void populateViewHolder(RecyclerView.ViewHolder viewHolder, Person model,
int position) {
if (viewHolder instanceof PersonType1ViewHolder) {
((PersonType1ViewHolder) viewHolder).name.setText("Full name: " + model.getName());
return;
}
if (viewHolder instanceof PersonType2ViewHolder) {
((PersonType2ViewHolder) viewHolder).name.setText("Full name: " + model.getName());
((PersonType2ViewHolder) viewHolder).tvAge.setText("Age: " + model.getAge());
}
}
private class PersonType1ViewHolder extends RecyclerView.ViewHolder {
TextView name;
PersonType1ViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.tvName);
}
}
private class PersonType2ViewHolder extends RecyclerView.ViewHolder {
TextView name;
TextView tvAge;
PersonType2ViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.tvName);
tvAge = (TextView) itemView.findViewById(R.id.tvAge);
}
}
@Override
public int getItemViewType(int position) {
return getItem(position).getType();
}
}
模特班(人)
public class Person {
public String name;
public int age;
public int type;
@IntDef({PersonType.TYPE_1, PersonType.TYPE_2 })
public @interface PersonType {
int TYPE_1 = 1;
int TYPE_2 = 2;
}
...
}
活动
PersonAdapter adapter = new PersonAdapter(Person.class, myFirebaseRef);
mRecyclerView.setAdapter(adapter);
我的Firebase数据库json看起来像
{
"students" : {
"-KgX6BkMc-zaxk6E7ohB" : {
"age" : 23,
"name" : "Linh",
"type" : 1
},
"-KgX6UCjrTYmPgFUrgTY" : {
"age" : 23,
"name" : "Linh",
"type" : 2
},
"-KgX7G9MdE8rm0PspZp3" : {
"age" : 23,
"name" : "Linh",
"type" : 1
},
"s1" : {
"age" : 31,
"name" : "John",
"type" : 2
},
"s2" : {
"age" : 31,
"name" : "John",
"type" : 1
},
"s3" : {
"age" : 31,
"name" : "John",
"type" : 2
},
"s4" : {
"age" : 31,
"name" : "John",
"type" : 2
}
}
}