I am trying to use table layout with recyclerView but it is giving the error:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TableRow.addView(android.view.View)' on a null object reference.
There is my adapter class:
public class CompanyAdapter extends RecyclerView.Adapter<CompanyAdapter.bookViewHolder> {
ArrayList<DataClass> arrayList;
TextView deposit;
TextView purchase;
TextView date;
TableRow tableRow;
TableLayout tableLayout;
public CompanyAdapter(ArrayList<DataClass> arrayList){
this.arrayList = arrayList;
}
@Override
public bookViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
View view =
LayoutInflater.
from(context).inflate(R.layout.company_list_item,parent,false);
return new bookViewHolder(view);
}
@Override
public void onBindViewHolder(bookViewHolder holder, int position) {
DataClass dataClass = arrayList.get(position);
holder.bind(dataClass);
//bookNameTV.setText(dataClass.getBook());
//authorNameTV.setText(dataClass.getAuthor());
}
@Override
public int getItemCount() {
return arrayList.size();
}
public class bookViewHolder extends RecyclerView.ViewHolder{
public bookViewHolder(View itemView) {
super(itemView);
deposit = (TextView) itemView.findViewById(R.id.deposit);
purchase = (TextView) itemView.findViewById(R.id.purchase);
date = (TextView) itemView.findViewById(R.id.date);
tableLayout = (TableLayout)
itemView.findViewById(R.id.maintablelayout);
}
public void bind(DataClass dataClass){
deposit.setText(Integer.toString(dataClass.getAmount()));
purchase.setText(Integer.toString(dataClass.getAmount()));
date.setText(dataClass.date);
tableRow.addView(deposit);
tableRow.addView(purchase);
tableRow.addView(date);
tableLayout.addView(tableRow, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));// giving error
}
}
}
One more question, Is there any better way to use tableLayout with recycler View without using any library?
Thanks..!