Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView is showing nothing

I have some items in my adapter but nothing is shown in the RecyclerView.

Adapter

public class WorkOrderAdapter extends RecyclerView.Adapter<WorkOrderViewHolder> {

    private List<WorkOrder> orders = new LinkedList<>();

    public void setData(List<WorkOrder> orders) {
        this.orders = orders;
    }

    @Override
    public WorkOrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_workorder, parent, false);
        return new WorkOrderViewHolder(view);
    }

    @Override
    public void onBindViewHolder(WorkOrderViewHolder holder, int position) {
        WorkOrder order = orders.get(position);
        holder.bind(order);
    }

    @Override
    public int getItemCount() {
        return orders.size();
    }

}

ViewHolder

public class WorkOrderViewHolder extends RecyclerView.ViewHolder {

    private TextView title;
    private TextView description;
    private TextView date;

    public WorkOrderViewHolder(View view) {
        super(view);
        title = (TextView) view.findViewById(R.id.title_textview);
        description = (TextView) view.findViewById(R.id.description_textview);
        date = (TextView) view.findViewById(R.id.date_textview);
    }

    public void bind(WorkOrder order) {
        title.setText("Test");
        description.setText("Test");
        date.setText("Test");
    }
}

Activity (Using AndroidAnnotations)

@EActivity(R.layout.activity_workorders)
@OptionsMenu(R.menu.activity_workorders)
public class WorkOrdersActivity extends ToolbarActivity {

    @ViewById(R.id.orders_recyclerview)
    RecyclerView ordersList;

    List<WorkOrder> orders = new LinkedList<>();
    private WorkOrderAdapter adapter;

    {
        adapter = new WorkOrderAdapter();
        orders.add(new WorkOrder());
        orders.add(new WorkOrder());
        orders.add(new WorkOrder());
        adapter.setData(orders);
    }

    @AfterViews
    public void initViews() {
        ordersList.setAdapter(adapter);
    }

}
like image 646
Héctor Avatar asked Nov 29 '22 23:11

Héctor


1 Answers

Please add the LayoutManager to the RecyclerView and try again

ordersList.setLayoutManager(new LinearLayoutManager(this));
ordersList.setAdapter(adapter);
like image 163
Jagadesh Seeram Avatar answered Dec 04 '22 14:12

Jagadesh Seeram