Shepherd Tsai Shepherd Tsai - 1 month ago 32x
Android Question

Android recyclerView findViewHolderForAdapterPosition returns null

I want to click a item in recyclerView programmatically, I found a way do that:


but it doesn't work for me, the findViewHolderForAdapterPosition just returns null.

Did I miss something in my code?


public class HistoryListAdapter extends RecyclerView.Adapter<HistoryListAdapter.ViewHolder> {
private static ArrayList<RecordItem> recordItems;
private static FragmentActivity activity;
private static RecordList recordList;

public HistoryListAdapter(ArrayList<RecordItem> recordItems, FragmentActivity FA, RecordList FRL) {
this.recordItems = recordItems;
this.activity = FA;
this.recordList = FRL;

public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recorditem, parent, false);
return new ViewHolder(itemLayoutView);

public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
// TextView setText ...

// Return the size of the itemsData (invoked by the layout manager)
public int getItemCount() {
return recordItems.size();

// inner class to hold a reference to each item of RecyclerView
public static class ViewHolder extends RecyclerView.ViewHolder{
public TextView result, datetime;

public ViewHolder(View itemLayoutView) {
itemLayoutView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (activity != null) {
// do something...
result = (TextView) itemLayoutView.findViewById(;
heartrate = (TextView) itemLayoutView.findViewById(;
datetime = (TextView) itemLayoutView.findViewById(;


RecyclerView listView = (RecyclerView) layoutView.findViewById(;
HistoryListAdapter listadapter = new HistoryListAdapter(itemsToShow, getActivity(), RecordList.this);
listView.swapAdapter(listadapter, false);

I omitted some code, But it should not affect the overall structure of my code.


According to the official documentation:

if notifyDataSetChanged() has been called but the new layout has not been calculated yet, this method will return null since the new positions of views are unknown until the layout is calculated.

It's not safe to use findViewHolderForAdapterPosition().

While you call this method after listView.swapAdapter(listadapter, false); you'll always get a null as result because notifyDataSetChanged() will be called.