Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 868
Adam Katz Avatar asked Jan 07 '17 14:01

Adam Katz


3 Answers

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

like image 72
Mohammed mansoor Avatar answered Sep 18 '22 22:09

Mohammed mansoor


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!!.

like image 40
Vahid Ghadiri Avatar answered Sep 20 '22 22:09

Vahid Ghadiri


@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>
like image 35
Kenny Wyland Avatar answered Sep 16 '22 22:09

Kenny Wyland