Two type models and FirebaseRecyclerAdapter in android

冷暖自知 提交于 2020-01-02 20:08:29

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/43112200/two-type-models-and-firebaserecycleradapter-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!