Adam Katz
Adam Katz

Reputation: 6962

Recycler view not calling getItemCount

I just made a recycler view and it was not working, so I put a breakpoint on getItemCount and the method is not being called. I have never seen anyone else on SO have that particular issue, I am sure it is something ridiculously obvious. Here is my code.

public class SearchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private ArrayList<Object> displayList;

public static class ViewHolder extends RecyclerView.ViewHolder {
    public ViewHolder(View v) {
        super(v);
    }
}

public class ErrorSearchItem extends ViewHolder {
    //this is here if there is no other viewholder

    public ErrorSearchItem(View view) {
        super(view);

    }
}

public class HeaderViewHolder extends ViewHolder {


    public HeaderViewHolder(View view) {
        super(view);

    }
}



@Override
public int getItemViewType(int position) {

    return position;
}

public SearchAdapter(ArrayList<Object> displayList) {
    this.displayList = displayList;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (displayList.get(viewType) instanceof String){
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.row_search_title, parent, false);
        return new HeaderViewHolder(itemView);
    }
    else{  //this is for if there is an error and no other xml files match
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.row_error_search_item, parent, false);
        return new ErrorSearchItem(itemView);
    }
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

}


@Override
public int getItemCount() {

    return displayList.size();
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);

}

}

edit: fragment code:

private RecyclerView recyclerView;
private SearchAdapter searchAdapter;

 recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);

    searchAdapter = new SearchAdapter(categorizedArray);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(searchAdapter);

Upvotes: 27

Views: 10654

Answers (3)

Kenny Wyland
Kenny Wyland

Reputation: 21880

@Mohammed's answer helped me realize that I needed to set a LayoutManager, but I was confused because I had looked at an auto-generated project that included a RecyclerView implementation and it didn't set the layout manager.

I discovered that you can set the layout manager in the xml as well:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutManager="LinearLayoutManager"
    >
</androidx.recyclerview.widget.RecyclerView>

Upvotes: 4

Mohammed mansoor
Mohammed mansoor

Reputation: 829

add recycler.setLayoutManager(new LinearLayoutManager(this)); before applying adapter

Upvotes: 51

Vahid Ghadiri
Vahid Ghadiri

Reputation: 4076

I know It's completely nonsense but for me the problem solved when I changed the RecylerView layout_width and layout_height property from match_parent to wrap_content!!.

Upvotes: 3

Related Questions