I simply made an adapter for my recyclerview.like this :
class CustomerAdapter(mCtx:Context,val customers:ArrayList<Customer>):RecyclerView.Adapter<CustomerAdapter.ViewHolder>(){
and i have a xml layout file to inflate recycleview items.
But the problem is in onCreateViewHolder
… I can not access inflated elements …
codes writed in my onCreateViewHolder method :
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomerAdapter.ViewHolder {
val v = LayoutInflater.from(parent.getContext()).inflate(R.layout.lo_customers, parent, false);
var txt = v.id
}
i can not access view elements by id .
4
Answers
At gradle app
Or
Then your Activity Import this
Even if you capture the
View
, you cannot directly access the children’s in theView
. In-order to get hold of children’s you need to usefindViewById(R.id.my_textView_id)
on theView
object.Hence replace your
v.txt
byv.findViewById(R.id.my_text_view_id)
.This is completely the wrong way to do it . onCreateViewHolder the function name itself says that it is meant only for inflating the view and doing nothing else. It should only be used for creating the ViewHolder . Even if you inflate your view and assign view from there , it is going to crash in the future . You have to inflate the view there and pass the parent view to another class which should extend RecyclerView.ViewHolder() .
So do it the following way :
Create an ViewHolder Class extending RecyclerView.ViewHolder :
Try not to access from android.R, rather try like this:
CategoryAdapterHolder(LayoutInflater.from(parent.context).inflate(com.example.secondhand.R.layout.category_list, parent, false))