RecyclerView isn't showed without setLayoutManager, but setLayoutManager makes my program crash. This method was demonstrated in all of the tutorials I have watched. Yeah, I know, there is too much code with a lot of garbage, but the error can be anywhere, so I hope you can help me(
StoreInfoPage.java (get it just like the MainActivity)
public class StoreInfoPage extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.store_info_page);
RecyclerView lastMonthSellsList = findViewById(R.id.last_month_sells_list);
ArrayList<Product> products = new ArrayList<>();
products.add(new Product(4, 5, true, true, 16000, 12000));
products.add(new Product(4, 5, true, true, 16000, 12000));
StoreSellingAdapter adapter = new StoreSellingAdapter(this, products);
lastMonthSellsList.setAdapter(adapter);
lastMonthSellsList.setLayoutManager(new LinearLayoutManager(this));
}
}
store_info_page.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".StoreInfoPage">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView41"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="bottom"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold|italic" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@android:color/transparent"
android:scaleType="fitEnd"
app:srcCompat="@android:drawable/ic_menu_info_details" />
</LinearLayout>
<View android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"
android:layout_marginBottom="10dp"/>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto_slab_bold"
android:text="Кофе 3в1"
android:textColor="#000000"
android:textSize="24sp"
android:textStyle="bold|italic" />
<EditText
android:id="@+id/editText5"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:ems="10"
android:fontFamily="@font/roboto_slab_bold"
android:inputType="number"
android:textAlignment="center" />
<TextView
android:id="@+id/textView17"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="@font/roboto_slab_bold"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto_slab_bold"
android:text="Чёрное Кофе"
android:textColor="#000000"
android:textSize="24sp"
android:textStyle="bold|italic" />
<EditText
android:id="@+id/editText6"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:ems="10"
android:fontFamily="@font/roboto_slab_bold"
android:inputType="number"
android:textAlignment="center" />
<TextView
android:id="@+id/textView18"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="@font/roboto_slab_bold"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto_slab_bold" />
<TextView
android:id="@+id/textView16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto_slab_bold"
android:textColor="#000000" />
<TextView
android:id="@+id/textView19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto_slab_bold"
android:textColor="#000000" />
</TableRow>
</TableLayout>
<Switch
android:id="@+id/switch1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:showText="true"
android:text="ДОЛГ"
android:textAlignment="center"
android:textSize="24sp" />
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto_slab_bold"
android:text="Добавить"
android:textSize="24sp" />
<View android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<TextView
android:id="@+id/textView20"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto_slab_bold"
android:text="ПРОШЛЫЙ МЕСЯЦ"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="30sp" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/last_month_sells_list"
/>
</LinearLayout>
store_sell_list_table_row.xml (Adapter xml for RecyclerView)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:id="@+id/sells_list_adapter_layout"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/whiteCoffeeAdapter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto_slab_bold"
android:text="Кофе 3в1"
android:textColor="#000000"
android:textSize="24sp" />
<TextView
android:id="@+id/whiteCoffeeQuantityAdapter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="@font/roboto_slab_bold"
android:text="0"
android:textColor="#000000" />
<TextView
android:id="@+id/whiteCoffeePriceAdapter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="@font/roboto_slab_bold"
android:text="0"
android:textColor="#000000" />
<TextView
android:id="@+id/whiteCoffeeTotalPriceAdapter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="@font/roboto_slab_bold"
android:text="0"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/blackCoffeeAdapter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto_slab_bold"
android:text="Чёрное Кофе"
android:textColor="#000000"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="@+id/blackCoffeeQuantityAdapter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="@font/roboto_slab_bold"
android:text="0"
android:textColor="#000000" />
<TextView
android:id="@+id/blackCoffeePriceAdapter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="@font/roboto_slab_bold"
android:text="0"
android:textColor="#000000" />
<TextView
android:id="@+id/blackCoffeeTotalPriceAdapter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="@font/roboto_slab_bold"
android:text="0"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView38"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto_slab_bold"
android:textColor="#000000" />
<TextView
android:id="@+id/textView39"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="@font/roboto_slab_bold"
android:textColor="#000000" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/coffeeTotalPriceAdapter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="@font/roboto_slab_bold"
android:text="0"
android:textColor="#000000" />
</TableRow>
</TableLayout>
<TextView
android:id="@+id/status_text_view_adapter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ОПЛАЧЕНО"
android:textAlignment="center"
android:textStyle="bold" />
</LinearLayout>
StoreSellingAdapter (Adapter class)
public class StoreSellingAdapter extends RecyclerView.Adapter<StoreSellingAdapter.ViewHolder>{
private ArrayList<Product> products;
private Context context;
StoreSellingAdapter(Context context, ArrayList<Product> products) {
this.products = products;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.store_sell_list_table_row, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Product product = products.get(position);
holder.whiteCoffeeQuantity.setText(product.getWhiteCoffeeQuantity());
holder.blackCoffeeQuantity.setText(product.getBlackCoffeeQuantity());
holder.whiteCoffeePrice.setText(product.getWhiteCoffeePrice());
holder.blackCoffeePrice.setText(product.getBlackCoffeePrice());
holder.whiteCoffeeTotalPrice.setText(product.getWhiteCoffeeTotalPrice());
holder.blackCoffeeTotalPrice.setText(product.getBlackCoffeeTotalPrice());
holder.coffeeTotalPrice.setText(product.getCoffeeTotalPrice());
if(product.isDebt() && product.isPaid()) {
holder.statusTextViewAdapter.setText("ДОЛГ(ОПЛАЧЕНО)");
holder.parentLayout.setBackgroundColor(Color.parseColor("#ef5350"));
}
}
@Override
public int getItemCount() {
return products.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
TextView whiteCoffeeQuantity;
TextView blackCoffeeQuantity;
TextView whiteCoffeePrice;
TextView blackCoffeePrice;
TextView whiteCoffeeTotalPrice;
TextView blackCoffeeTotalPrice;
TextView coffeeTotalPrice;
TextView statusTextViewAdapter;
LinearLayout parentLayout;
ViewHolder(@NonNull View itemView) {
super(itemView);
whiteCoffeeQuantity = itemView.findViewById(R.id.whiteCoffeeQuantityAdapter);
blackCoffeeQuantity = itemView.findViewById(R.id.blackCoffeeQuantityAdapter);
whiteCoffeePrice = itemView.findViewById(R.id.whiteCoffeePriceAdapter);
blackCoffeePrice = itemView.findViewById(R.id.blackCoffeePriceAdapter);
whiteCoffeeTotalPrice = itemView.findViewById(R.id.whiteCoffeeTotalPriceAdapter);
blackCoffeeTotalPrice = itemView.findViewById(R.id.blackCoffeeTotalPriceAdapter);
coffeeTotalPrice = itemView.findViewById(R.id.coffeeTotalPriceAdapter);
parentLayout = itemView.findViewById(R.id.sells_list_adapter_layout);
statusTextViewAdapter = itemView.findViewById(R.id.status_text_view_adapter);
}
}
}
Product.java
public class Product {
private int whiteCoffeeQuantity;
private int blackCoffeeQuantity;
private boolean isDebt;
private boolean isPaid;
private int whiteCoffeePrice;
private int blackCoffeePrice;
private int whiteCoffeeTotalPrice;
private int blackCoffeeTotalPrice;
private int coffeeTotalPrice;
public Product(int whiteCoffeeQuantity, int blackCoffeeQuantity, boolean isDebt, boolean isPaid, int whiteCoffeePrice, int blackCoffeePrice) {
this.whiteCoffeeQuantity = whiteCoffeeQuantity;
this.blackCoffeeQuantity = blackCoffeeQuantity;
this.isDebt = isDebt;
this.isPaid = isPaid;
this.whiteCoffeePrice = whiteCoffeePrice;
this.blackCoffeePrice = blackCoffeePrice;
whiteCoffeeTotalPrice = whiteCoffeeQuantity*whiteCoffeePrice;
blackCoffeeTotalPrice = blackCoffeeQuantity*blackCoffeePrice;
coffeeTotalPrice = whiteCoffeeTotalPrice+blackCoffeeTotalPrice;
}
public int getWhiteCoffeeQuantity() {
return whiteCoffeeQuantity;
}
public void setWhiteCoffeeQuantity(int whiteCoffeeQuantity) {
this.whiteCoffeeQuantity = whiteCoffeeQuantity;
}
public int getBlackCoffeeQuantity() {
return blackCoffeeQuantity;
}
public void setBlackCoffeeQuantity(int blackCoffeeQuantity) {
this.blackCoffeeQuantity = blackCoffeeQuantity;
}
public boolean isDebt() {
return isDebt;
}
public void setDebt(boolean debt) {
isDebt = debt;
}
public boolean isPaid() {
return isPaid;
}
public void setPaid(boolean paid) {
isPaid = paid;
}
public int getWhiteCoffeePrice() {
return whiteCoffeePrice;
}
public void setWhiteCoffeePrice(int whiteCoffeePrice) {
this.whiteCoffeePrice = whiteCoffeePrice;
}
public int getBlackCoffeePrice() {
return blackCoffeePrice;
}
public void setBlackCoffeePrice(int blackCoffeePrice) {
this.blackCoffeePrice = blackCoffeePrice;
}
public int getWhiteCoffeeTotalPrice() {
return whiteCoffeeTotalPrice;
}
public void setWhiteCoffeeTotalPrice(int whiteCoffeeTotalPrice) {
this.whiteCoffeeTotalPrice = whiteCoffeeTotalPrice;
}
public int getBlackCoffeeTotalPrice() {
return blackCoffeeTotalPrice;
}
public void setBlackCoffeeTotalPrice(int blackCoffeeTotalPrice) {
this.blackCoffeeTotalPrice = blackCoffeeTotalPrice;
}
public int getCoffeeTotalPrice() {
return coffeeTotalPrice;
}
public void setCoffeeTotalPrice(int coffeeTotalPrice) {
this.coffeeTotalPrice = coffeeTotalPrice;
}
}