Reputation: 32
I have a recyclerView. I need to fill with different types of input types so I tried putting this line after the recyclerView is made:
((EditText)recyclerView.findViewHolderForAdapterPosition(6)
.itemView.findViewById(R.id.editText))
.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
Works fine if it's done after a button click, but if just put on the onStart() method like below:
loadrecyclerView(); //(fills the recyclerView with items)
((EditText)recyclerView.findViewHolderForAdapterPosition(6)
.itemView.findViewById(R.id.editText))
.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
It doesn't work and sends the error code:
Attempt to read from field 'android.view.View androidx.recyclerview.widget.RecyclerView$ViewHolder.itemView' on a null object reference
Upvotes: 0
Views: 631
Reputation: 19263
RecyclerView
takes some time to draw items, they aren't ready one line after setting adapter
or calling notifyDataSetChanged
(or similar)
one of the ways is to wait until RecyclerView
draw all its children, using post
recyclerView.post(
new Runnable(){
// code in here will fire when recyclerView finishes all its jobs
// e.g. redrawing
}
);
but its kind of hack, should be used only at least. in your case setInputType
should be used inside adapter
in onCreateViewHolder
or maybe in onBindViewHolder
, or even straight inside list item XML
Upvotes: 2