d中的两个类型模型和FirebaseRecyclerAdapte

d中的两个类型模型和FirebaseRecyclerAdapte

本文介绍了Android中的两个类型模型和FirebaseRecyclerAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的firebase数据库中有两种类型的产品,它们是以下几种。



型号:ProductTypeOne.java

  package fyp.hkust.facet.model; 

import java.util.ArrayList;

public class ProductTypeOne {

private String name;
私人字符串品牌;
private String desc;
private String image;
私人字符串用户名;
private String uid;
private ArrayList< ArrayList< String>>颜色;

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 =品牌;
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;

$ b $ public void setUid(String uid){
this.uid = uid;
}

public ArrayList< ArrayList< String>> getColor(){
返回颜色;


public void setColor(ArrayList< ArrayList< String>> color){
this.color = color;


$ b code
$ b

Model:ProductTypeTwo.java p>

  package fyp.hkust.facet.model; 

import java.util.ArrayList;

public class ProductTypeTwo {

private String name;
私人字符串品牌;
private String desc;
private String image;
私人字符串用户名;
private String uid;
private ArrayList< String>颜色;

public Product()
{

}

public Product(String name,String brand,String desc,String image,String username,String uid,ArrayList< String> color){
this.name = name;
this.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;

$ b $ public void setUid(String uid){
this.uid = uid;
}

public ArrayList< String> getColor(){
返回颜色;


public void setColor(ArrayList< String> color){
this.color = color;


$ b code
$ b



有一个指标可以检测出他们的类型。我如何检测他们的类型来选择合适的模型来获取他们?请给我一些帮助。

  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 $ b $ 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);


解决方案

像这样创建自定义适配器

 类PersonAdapter扩展了FirebaseRecyclerAdapter< Person,RecyclerView.ViewHolder> {

PersonAdapter(Class< Person> modelClass,DatabaseReference ref){
super(modelClass,0,RecyclerView.ViewHolder.class,ref);

$ b @Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
switch(viewType){$ b $ case Person.PersonType。 TYPE_1:
View userType1 = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_person_type_1,parent,false);
返回新的PersonType1ViewHolder(userType1);
case Person.PersonType.TYPE_2:
View userType2 = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_person_type_2,parent,false);
返回新的PersonType2ViewHolder(userType2);
}
返回super.onCreateViewHolder(parent,viewType);

$ b $覆盖
保护无效populateViewHolder(RecyclerView.ViewHolder viewHolder,Person模型,
int位置){
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(查看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();

$ b

模型类(Person)

  public class Person {
public String name;
public int age;
public int type;
$ b $ @ @Dent({PersonType.TYPE_1,PersonType.TYPE_2})
public @interface PersonType {
int TYPE_1 = 1;
int TYPE_2 = 2;



code

$ b

Activity


$ b

  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:J ohn,
type:1
},
s3:{
age:31,
name:John,
type:2
},
s4:{
age:31,
name:John,
键入:



$ c $

I have two types of product in my firebase db which are the following.

Model : 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;
}

}

Model : 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;
}

}

There is an indicator for me to detect that their type. How can I detect their type to choose the right model to get them? Please give me some helps. Thank you very much.

 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);
解决方案

Create Custom Adapter like this

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();
    }
}

Model class (Person)

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;
    }
    ...
}

In Activity

PersonAdapter adapter = new PersonAdapter(Person.class, myFirebaseRef);
mRecyclerView.setAdapter(adapter);

FULL DEMO

My Firebase database json will look like

{
  "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
    }
  }
}

这篇关于Android中的两个类型模型和FirebaseRecyclerAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 05:57