1

I have two list<> With different data but that are equal in size , I want to use these two lists in one RecyclerView And I want to fill RecyclerView items using these two lists, but items that are filled with the second list are empty in RecyclerView and Just Items that are filled with the first list are show.

i dont know how to use both List in one recyclerView.

RecyclerViewAdapter :

public class RecyclerViewAdapter_StudentList extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

private List<StudentTable> studentData = new ArrayList<>();//First List

private List<PerformanceTable> performanceData = new ArrayList<>();//Second List

private Context context;

private DatabaseHandler database;

public RecyclerViewAdapter_StudentList(Context context , List<StudentTable> tableData , List<PerformanceTable> performanceData)
{
    this.context = context;

    this.studentData = tableData;

    this.performanceData = performanceData;

    database = new DatabaseHandler(context);
}
    public class itemHolder extends RecyclerView.ViewHolder
{
    TextView studentName ;
    TextView studentPositive;
    TextView studentNegative;

    public itemHolder(View itemView)
    {
        super(itemView);

        studentName = (TextView) itemView.findViewById(R.id.item_txt_StudentList_StudentName);
        studentPositive = (TextView) itemView.findViewById(R.id.item_StudentList_Positive);
        studentNegative = (TextView) itemView.findViewById(R.id.item_StudentList_Negative);

    }
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(context).inflate(R.layout.recyclerview_item_student_list, parent, false);

    return new itemHolder(view);
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

    final itemHolder itemHolder = (itemHolder) holder;

    final String studentName = studentData.get(position).getStudentName();

    itemHolder.studentName.setText(String.valueOf(studentName));

    int pCount = performanceData.get(position).getPositive();
    int nCount = performanceData.get(position).getNegative();

    itemHolder.studentPositive.setText(String.valueOf(pCount));//These items are empty

    itemHolder.studentNegative.setText(String.valueOf(nCount));//These items are empty

.
.
.
}

    public int getItemCount()
    {
         return studentData.size();
    }

2 Answers2

0

@Nima Khalili please go through below links this might what you r asking for.

https://stackoverflow.com/a/26245463/4361438

https://www.journaldev.com/12372/android-recyclerview-example

i will explain it if this will fix your problem.

0

You can do something like this....

List<Model1> list1 = new ArrayList<>();
List<Model2> list2 = new ArrayList<>();
List<BothModels> list3 = new ArrayList<>();

list1.add(new Model1(whatever here));
//add more to list1
list2.add(new Model2(whatever here));
//add more to list2

list3.addAll(list1);
list3.addAll(list2);

Now that everything is in list3, you can use that list to fill in your recycler view.

Its also possible to achieve it with polymorphism... using an interface. Might actually be a better way now that I think about it. If you have no idea what polymorphism is or if you never created an interface before, I would recommend reading up on it. I wont cover that in this answer.

Here is also another answer this is way better....

https://stackoverflow.com/a/13706974/8200290

letsCode
  • 2,774
  • 1
  • 13
  • 37