Sam Sam - 5 months ago 76
Android Question

Child RecyclerView's onBindViewHolder doesn't work on my Nested RecyclerView Project in Android

I have two RecyclerViews. Parent RecyclerView works perfectly but child's onBindViewHolder does not work properly. It does not executes on scroll hence my textboxes are not updating as expected.

Could anyone help me to get executed child RecyclerView's onBindViewHolder on every scroll?

Below is the piece of code for better understanding.

MainActivity.java

RecyclerView recyclerView;
HistoryMainAdapter adapter;
conn = new ConnectionClass();
int pid=1,month=05,year=2016;
datalistMain = conn.getHistoryDetails(pid,month,year);
adapter=new HistoryMainAdapter(datalistMain,dateString,count,getApplicationContext());
RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);


HistoryMainAdapter.java(parent)

public class HistoryMainAdapter extends RecyclerView.Adapter<HistoryMainAdapter.MyViewHolder> {

private LayoutInflater inflater;
MyViewHolder holder;
Context contextmain;
ArrayList<tblExamHistory> datalistMain = new ArrayList<>();
ArrayList<tblExamHistory> datalistSub = new ArrayList<>();
ArrayList<String> dateString=new ArrayList<>();
RecyclerView recyclerView;
HistorySubAdapter adapter;
String date,prevday,day;
int count,cnt=0,k=0;


public HistoryMainAdapter(ArrayList<tblExamHistory> datalistMain,ArrayList<String> dateString, int count, Context context)
{
inflater=LayoutInflater.from(context);
this.datalistMain=datalistMain;
this.count=count;
this.dateString=dateString;
contextmain=context;
date=datalistMain.get(0).getAttendedDate();
day = date.substring(Math.max(date.length() - 2, 0)); //To get last two digits(day) of date
prevday="00";
}

@Override
public HistoryMainAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.history_main, parent, false);
holder = new MyViewHolder(view);
return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
int c=1;
holder.txtView.setText(dateString.get(position));
}

@Override
public int getItemCount() {
return count;
}

public class MyViewHolder extends RecyclerView.ViewHolder {
TextView txtView;
public MyViewHolder(View itemView) {
super(itemView);
txtView=(TextView)itemView.findViewById(R.id.txtDate);
tblExamHistory data = new tblExamHistory();
int init=0;
addRecyclers(init,itemView,data);
}
}
public void addValues(int x,View itemView,tblExamHistory data)
{
data.setAttendedDate(datalistMain.get(x).getAttendedDate());
data.setExamTitle(datalistMain.get(x).get_ExamTitle());
data.setStartTime(datalistMain.get(x).getStartTime());
datalistSub.add(data);
recyclerView = (RecyclerView) itemView.findViewById(R.id.recyclerView1);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(contextmain);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
adapter = new HistorySubAdapter(datalistSub, contextmain);
recyclerView.setAdapter(adapter);
}


HistorySubAdapter.java(child)

public class HistorySubAdapter extends RecyclerView.Adapter<HistorySubAdapter.MyViewHolder> {

MyViewHolder holder;
LayoutInflater inflater;
ArrayList<tblExamHistory> datalist;

public HistorySubAdapter(ArrayList<tblExamHistory> datalist, Context context)
{
inflater= LayoutInflater.from(context);
this.datalist=datalist;
}
@Override
public HistorySubAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.history_sub, parent, false);
holder = new MyViewHolder(view);
return holder;
}

@Override
public void onBindViewHolder(HistorySubAdapter.MyViewHolder holder, int position) {

> //this triggers only on initialisation
1231
> holder.txtTime.setText(datalist.get(position).getStartTime());
> holder.txtTitle.setText(datalist.get(position).get_ExamTitle());

}

@Override
public int getItemCount() {
return datalist.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {
TextView txtTime,txtTitle;
public MyViewHolder(View itemView) {
super(itemView);
txtTime=(TextView) itemView.findViewById(R.id.txtTime);
txtTitle=(TextView) itemView.findViewById(R.id.txtExamName);
}
}
}


Screenshots here :-
Screenshot 1
Screenshot 2

Sam Sam
Answer

Replaced following piece of code from HistoryMainAdapter.Java. Because I forget to add a single line of code i.e; tblExamHistory data= new tblExamHistory(); That is why recyleview got same value for all the rows. Now it works fine.

 public class MyViewHolder extends RecyclerView.ViewHolder {
            TextView txtView;
            public MyViewHolder(View itemView) {
                super(itemView);
                txtView=(TextView)itemView.findViewById(R.id.txtDate);
                tblExamHistory data = new tblExamHistory();
                int init=0;
                addRecyclers(init,itemView,data);
                recyclerView = (RecyclerView) itemView.findViewById(R.id.recyclerView1);
                RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(contextmain);
                recyclerView.setLayoutManager(layoutManager);
                recyclerView.setItemAnimator(new DefaultItemAnimator());
                adapter = new HistorySubAdapter(datalistSub, contextmain);
                recyclerView.setAdapter(adapter);
              }
           }
         public void addValues(int x)
            {
                tblExamHistory data= new tblExamHistory();
                data.setAttendedDate(datalistMain.get(x).getAttendedDate());
                data.setExamTitle(datalistMain.get(x).get_ExamTitle());
                data.setStartTime(datalistMain.get(x).getStartTime());
                datalistSub.add(data);
            }
Comments