Ian Lee Ian Lee - 6 months ago 23
Android Question

RecyclerView Empty

My recycler view is empty, I can tell its there because I can scroll left and right(indicators on the edges of the screen tells me so) but my individual lists item is not showing up.

Here is my code for main activity

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_split_order);
android.support.v7.app.ActionBar mActionBar = getSupportActionBar();
mActionBar.setDisplayHomeAsUpEnabled(true);

setTitle("Split Order");

//Code to get Screen Width and Height
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
int onequaterscreen = width / 4;
int onefifthheight = height/ 5;


recList = (RecyclerView) findViewById(R.id.rv_bill_list);
recList.setHasFixedSize(true);
createData();
adapter = new BillListAdapter(this,data);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.HORIZONTAL);
recList.setLayoutManager(llm);
recList.setAdapter(adapter);

}

public void createData(){

data = new ArrayList<>();
BillItem dummy = new BillItem();
dummy.setDiscountAmt(0.0);
dummy.setOrderNum(44);
dummy.setSubtotal(0.0);
data.add(dummy);
}


Adapter

private LayoutInflater inflator;
List<BillItem> billItemList = Collections.emptyList();

public BillListAdapter(Context context,List<BillItem> billItemList) {

inflator = LayoutInflater.from(context);
this.billItemList = billItemList;

}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View view = inflator.inflate(R.layout.bill_list_item,parent,false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

BillItem current = billItemList.get(position);
holder.tv_bill_subtotal.setText(String.valueOf(current.subtotal));
holder.tv_bill_discount_amt.setText(String.valueOf(current.discountAmt));
holder.tv_bill_num.setText(String.valueOf(current.orderNum));

}

@Override
public int getItemCount() {
return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder{

TextView tv_bill_subtotal;
TextView tv_bill_discount_amt;
TextView tv_bill_num;
RelativeLayout rl_bill_discount;
ListView lv_orderItemList;

public MyViewHolder(View itemView) {
super(itemView);
tv_bill_num = (TextView) itemView.findViewById(R.id.tv_bill_num);
tv_bill_subtotal = (TextView) itemView.findViewById(R.id.tv_bill_subtotal);
rl_bill_discount = (RelativeLayout) itemView.findViewById(R.id.rl_bill_discount);
lv_orderItemList = (ListView) itemView.findViewById(R.id.lv_orderitemlist);
tv_bill_discount_amt = (TextView) itemView.findViewById(R.id.tv_bill_discount_amt);
}
}


The layout file is

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent">

<RelativeLayout
android:id="@+id/rl_bill_sidemenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="false"
android:layout_alignParentLeft="false"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="@color/colorWhiteBrighter">

<TextView
android:paddingLeft="@dimen/sidemenu_horizontal_margin"
android:paddingRight="@dimen/sidemenu_horizontal_margin"
android:paddingTop="@dimen/sidemenu_vertical_margin"
android:id="@+id/tv_bill_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/ordermenunum"
android:textSize="20sp"
/>


<!--
Order List Header
-->
<RelativeLayout
android:id="@+id/rl_billitemlistheader"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="@dimen/sidemenu_horizontal_margin"
android:paddingRight="@dimen/sidemenu_horizontal_margin"
android:layout_marginTop="@dimen/sidemenu_vertical_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_bill_num"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@color/colorWhiteHighlight">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ordermenuqty"
android:textSize="14sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:text="@string/ordermenuname"
android:textSize="14sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/ordermenuprice"
android:textSize="14sp"/>


</RelativeLayout>

<ListView
android:layout_below="@id/rl_billitemlistheader"
android:id="@+id/lv_orderitemlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/rl_subtotal">

</ListView>




<!--
Pay Button
-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:padding="@dimen/sidemenu_vertical_margin"
android:background="@drawable/greenbutton_selector"
android:id="@+id/rl_bill_pay"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

<TextView

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28sp"
android:textColor="@color/colorPrimaryDark"
android:text=">>"/>

<TextView

android:id="@+id/tv_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28sp"
android:textColor="@color/colorWhiteBrighter"
android:text="@string/zerodollars"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />


</RelativeLayout>

<!--
Subtotal Field
-->
<RelativeLayout
android:id="@+id/rl_subtotal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:paddingLeft="@dimen/sidemenu_horizontal_margin"
android:paddingRight="@dimen/sidemenu_horizontal_margin"
android:background="@drawable/submenu_selector"
android:layout_above="@+id/rl_bill_discount">

<TextView

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="Subtotal:"/>

<TextView
android:id="@+id/tv_bill_subtotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="@string/zerodollars"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<View android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@color/colorWhiteDarker"
android:layout_below="@+id/rl_subtotal"
/>

<!--
Discount Field
-->
<RelativeLayout
android:id="@+id/rl_bill_discount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:paddingLeft="@dimen/sidemenu_horizontal_margin"
android:paddingRight="@dimen/sidemenu_horizontal_margin"
android:background="@drawable/submenu_selector"
android:layout_above="@+id/rl_bill_pay">

<TextView

android:id="@+id/tv_discount_lbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="Discount($):"/>

<TextView
android:id="@+id/tv_bill_discount_amt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="@string/zerodollars"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

</RelativeLayout>
<View android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@color/colorWhiteDarker"
android:layout_below="@+id/rl_subtotal"
/>


</RelativeLayout>




I used the debugger to check the values, nothing is null. Is just not inflating the view I guess cause there no list item.

Answer

The getItemCount method is returning 0

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

Return the Size of the Arraylist which you pass to the adapter class