I want to make cardview
list, I already create the XML for RecyclerView
and the item also with the the adapter and the model. But when I run it, it show nothing just blank screen.
This is my fragment :
public class OrderListFragment extends Fragment {
private RecyclerView recyclerView;
private OrderListAdapter adapter;
private ArrayList<OrderListModel> OrderListArrayList;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_order_list, container, false);
getActivity().setTitle(R.string.title_mn_order_list);
recyclerView = (RecyclerView) view.findViewById(R.id.orderList_recycler_view);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this.getActivity());
recyclerView.setLayoutManager(layoutManager);
adapter = new OrderListAdapter(OrderListArrayList);
recyclerView.setAdapter(adapter);
return view;
}
void addData(){
OrderListArrayList = new ArrayList<>();
OrderListArrayList.add(new OrderListModel(
"4 Juli 2018 12:20",
"ODRNO1804120003",
"Arief Wijaya Putra",
"MOBIL BARU",
"Mobil Baru HONDA BR-V-E CVT"));
OrderListArrayList.add(new OrderListModel(
"4 Juli 2018 10:54",
"ODRNO1804100001",
"Gusman Linandry",
"MOTOR BEAS",
"Motor Bekas Honda Supra X"));
OrderListArrayList.add(new OrderListModel(
"4 Juli 2018 11:54",
"ODRNO1804120005",
"Arie Fengki",
"MOTOR BARU",
"Motor Baru Yamaha N-Max 155"));
OrderListArrayList.add(new OrderListModel(
"3 Juli 2018 13:18",
"ODRNO1804110018",
"Bastian Anggara",
"MOBIL BEKAS",
"Mobil Bekas Toyota Avanza 1 3 G 2011"));
OrderListArrayList.add(new OrderListModel(
"11 Juni 2018 13:52",
"ODRNO1804110005",
"Tio",
"MOBIL BARU",
"Mobil Baru Mitsubishi Xpander Ultimate A/T"));
OrderListArrayList.add(new OrderListModel(
"11 Juni 2018 14:50",
"ODRNO1804110006",
"Mariska",
"MOTOR BARU",
"Motor Baru HONDA Verza 150"));
OrderListArrayList.add(new OrderListModel(
"11 Apr 2018 15:01",
"ODRNO1804110010",
"Dixie",
"MOBIL BEKAS",
"Mobil Bekas Xenia type X 1300cc 2016"));
}
}
This is my OrderListFragment :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
android:background="@drawable/actionbar_background"></View>
<android.support.v7.widget.RecyclerView
android:id="@+id/orderList_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
</LinearLayout>
This is my Model :
public class OrderListModel {
private String date;
private String orderNumber;
private String customerName;
private String productStatus;
private String notes;
public OrderListModel(String date, String orderNumber, String customerName, String productStatus, String notes) {
this.date = date;
this.orderNumber = orderNumber;
this.customerName = customerName;
this.productStatus = productStatus;
this.notes = notes;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getProductStatus() {
return productStatus;
}
public void setProductStatus(String productStatus) {
this.productStatus = productStatus;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
}
This is my OrderListItem XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="6dp"
card_view:cardElevation="3dp"
card_view:cardUseCompatPadding="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="@+id/txt_order_date"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txt_order_number"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txt_customer_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txt_product_status"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txt_notes"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
This is my adapter :
public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.OrderListViewHolder> {
private ArrayList<OrderListModel> itemOrderList;
public OrderListAdapter(ArrayList<OrderListModel> itemOrderList) {
this.itemOrderList = itemOrderList;
}
@Override
public OrderListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.order_list_item, parent, false);
return new OrderListViewHolder(view);
}
@Override
public void onBindViewHolder(OrderListViewHolder orderListViewHolder, int position) {
orderListViewHolder.txtDate.setText(itemOrderList.get(position).getDate());
orderListViewHolder.txtOrderNumber.setText(itemOrderList.get(position).getOrderNumber());
orderListViewHolder.txtCustomerName.setText(itemOrderList.get(position).getCustomerName());
orderListViewHolder.txtProductStatus.setText(itemOrderList.get(position).getProductStatus());
orderListViewHolder.txtNotes.setText(itemOrderList.get(position).getNotes());
}
@Override
public int getItemCount() {
return (itemOrderList != null) ? itemOrderList.size() : 0;
}
public class OrderListViewHolder extends RecyclerView.ViewHolder{
private TextView txtDate, txtOrderNumber, txtCustomerName, txtProductStatus,
txtNotes;
public OrderListViewHolder(View itemView) {
super(itemView);
txtDate = (TextView) itemView.findViewById(R.id.txt_order_date);
txtOrderNumber = (TextView) itemView.findViewById(R.id.txt_order_number);
txtCustomerName = (TextView) itemView.findViewById(R.id.txt_customer_name);
txtProductStatus = (TextView) itemView.findViewById(R.id.txt_product_status);
txtNotes = (TextView) itemView.findViewById(R.id.txt_notes);
}
}
}
5条答案
按热度按时间e4yzc0pl1#
Isue1您回收站视图高度为
match_parent
Isue2您没有在
onCreate
方法中调用addData()
方法。将回收器项目的高度保持为
match_parent
,每个屏幕仅显示一个视图。和将回收器项目的高度从match_parent
更改为wrap_content
。并在
onCreate
方法中调用addData()
方法。b1payxdu2#
你在哪里调用
add()
方法?我在你的代码中也没有看到它。在调用add之后,你应该将OrderListArrayList设置为适配器,然后调用adapter.norifyDataSetChanged();
h79rfbju3#
在OrderListItem主线性布局中将
android:layout_height="match_parent"
更改为wrap_content
2vuwiymt4#
cu6pst1q5#
也许您应该覆盖onViewCreate()