2

Let say, I have a student, employee and car class in a different structure in JSON file.

I already parse them and put the respective data to its POJO classes. The things is I wanna display data to inside a recycler view. But here I have common field both three class is name and weight.

So, I wanna pass to list of generic to recycler view and populate them by calling like this:

tvName.setText(Object(should be generic).getName());
tvWeight.setText(Object(should be generic).getWeight());

It should display name and weight all the student, employee and car .

RecyclerView looks like

---------------------------------------------------------
CarName    
CarWeight
---------------------------------------------------------
EmplyoeeName    
EmplyoeeWeight
---------------------------------------------------------
StudentName    
StudentWeight
---------------------------------------------------------
EmplyoeeName    
EmplyoeeWeight
---------------------------------------------------------
CarName    
CarWeight
---------------------------------------------------------
CarName    
CarWeight
---------------------------------------------------------
StudentName    
StudentWeight

Any idea would be highly appreciated.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Horrorgoogle
  • 7,858
  • 11
  • 48
  • 81

2 Answers2

3

In order to achieve that, you need something called polymorphism, learn more from StackOverflow, Java Docs and Wikipedia. In order to respect that pattern I would implement the problem like this:

I would create an Interface that has the methods you need:

public interface AttributesInterface {
    String getName();
    double getWeight();
}

Then I would make every POJO class implement that interface, looking in the end like this:

public class Car implements AttributesInterface {
    private String name;
    private double weight;

    @Override
    public String getName() {
        return null;
    }

    @Override
    public double getWeight() {
        return weight;
    }
}

In the adapter you store the list like this. If a class will implement the interface, then you will be able to add it in that array. So you will have an array that will contain Student, Car, Employee in the same time.

private List<AttributesInterface> list = new ArrayList<>();

Then final step is in onBindViewHolder where you get an object from that array and set the corresponding values.

AttributesInterface object = list.get(position);
tvName.setText(object.getName());
tvWeight.setText(String.valueOf(object.getWeight())); 

Also, you mentioned that you want a solution to work with multiple classes. As long as you implement the interface in each class that needs to be displayed, you can have a million classes.

Iulian Popescu
  • 2,595
  • 4
  • 23
  • 31
0

You can create only one POJO class and you can add extra variable say type. So your POJO class will look like below.

public class MyClassModel {

    private String type=""; // S=Student, C=Car, E=Employee
    private String  name="", weight=""; 

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getWeight() {
        return weight;
    }

    public void setWeight(String weight) {
        this.weight = weight;
    }   
}

Now you will get type in your RecyclerviewAdapter so you can write your logic according to type of data.

Viraj Patel
  • 2,113
  • 16
  • 23
  • This would work only if all classes have the same fields. If a `Student` will have some grades and a `Employee` will have a salary, then you can't use it like this. – Iulian Popescu Feb 09 '18 at 08:40