英文:
Recyclerview independent buttons and counter
问题
package com.nfc.netvision;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.ArrayList;
public class OrderActivity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<ModelOrder> orderArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
recyclerView = findViewById(R.id.recyclerview_order_scroll);
orderArrayList = new ArrayList<>();
orderArrayList.add(new ModelOrder(R.drawable.coke, "Coka Cola", "Kaltes Getränml", "6"));
orderArrayList.add(new ModelOrder(R.drawable.fastfood, "Coka Cola", "Kaltes Getränml", "10"));
orderArrayList.add(new ModelOrder(R.drawable.water, "Coka Cola", "Kaltes Getränml", "20"));
orderArrayList.add(new ModelOrder(R.drawable.burger, "Coka Cola", "Kaltes Getränml", "30"));
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
RecyclerView.LayoutManager recLiLayoutManager = layoutManager;
recyclerView.setLayoutManager(recLiLayoutManager);
OrderAdapter adapter = new OrderAdapter(this, orderArrayList);
recyclerView.setAdapter(adapter);
}
}
package com.nfc.netvision;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.ViewHolder> {
int value = 0; //Global
private Context mContext;
private ArrayList<ModelOrder> nList;
OrderAdapter(Context context, ArrayList<ModelOrder> list) {
mContext = context;
nList = list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
View view = layoutInflater.inflate(R.layout.recyclerview_order_items, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
ModelOrder orderItem = nList.get(position);
ImageView image = holder.item_image;
TextView name, place, price;
name = holder.item_name;
place = holder.item_place;
price = holder.item_price;
image.setImageResource(orderItem.getImage());
name.setText(orderItem.getName());
place.setText(orderItem.getPlace());
price.setText(orderItem.getPrice());
holder.order_item_minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
value = value - 1;
holder.order_item_count.setText("" + value);
}
});
holder.order_item_plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
value = value + 1;
holder.order_item_count.setText("" + value);
}
});
}
@Override
public int getItemCount() {
return nList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView item_image;
TextView item_name, item_place, item_price, order_item_minus, order_item_count, order_item_plus;
public ViewHolder(@NonNull View itemView) {
super(itemView);
item_image = itemView.findViewById(R.id.order_item_image);
item_name = itemView.findViewById(R.id.order_item_name);
item_place = itemView.findViewById(R.id.order_item_place);
item_price = itemView.findViewById(R.id.order_item_price);
order_item_minus = itemView.findViewById(R.id.order_item_minus);
order_item_plus = itemView.findViewById(R.id.order_item_plus);
order_item_count = itemView.findViewById(R.id.order_item_count);
}
}
}
<!-- recyclerViewer.xml -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
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="wrap_content"
android:layout_margin="10dp">
<!-- Contents of the XML layout -->
</androidx.cardview.widget.CardView>
<!-- activity_order.xml -->
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview_order_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- RecyclerView content -->
</androidx.recyclerview.widget.RecyclerView>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
英文:
I have a problem. The 2 buttons (+ and -) have the same counter. When I for example increase coke (lets say we want 2 cokes) i press the button 2 times and everything works, but as soon as i want french fries ( 1 french fries in addition to the 2 cokes) the counter at french fries increases to 3 whereas it should be 1 because i clicked it the first time. The following picture showes the problem.
The following xml and java classfile reconstruct the image you are seeing. Maybe you can help me out, i would be very thankful.
OrderActivity.java
package com.nfc.netvision;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.ArrayList;
public class OrderActivity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<ModelOrder> orderArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
recyclerView = findViewById(R.id.recyclerview_order_scroll);
orderArrayList = new ArrayList<>();
orderArrayList.add(new ModelOrder(R.drawable.coke, "Coka Cola", "Kaltes Getränml", "6"));
orderArrayList.add(new ModelOrder(R.drawable.fastfood, "Coka Cola", "Kaltes Getränml", "10"));
orderArrayList.add(new ModelOrder(R.drawable.water, "Coka Cola", "Kaltes Getränml", "20"));
orderArrayList.add(new ModelOrder(R.drawable.burger, "Coka Cola", "Kaltes Getränml", "30"));
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
RecyclerView.LayoutManager recLiLayoutManager = layoutManager;
recyclerView.setLayoutManager(recLiLayoutManager);
OrderAdapter adapter = new OrderAdapter(this, orderArrayList);
recyclerView.setAdapter(adapter);
}
}
OrderAdapter.java
package com.nfc.netvision;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.ViewHolder> {
int value = 0; //Global
private Context mContext;
private ArrayList<ModelOrder> nList;
OrderAdapter(Context context, ArrayList<ModelOrder> list) {
mContext = context;
nList = list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
View view = layoutInflater.inflate(R.layout.recyclerview_order_items, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
ModelOrder orderItem = nList.get(position);
ImageView image = holder.item_image;
TextView name, place, price;
name = holder.item_name;
place = holder.item_place;
price = holder.item_price;
image.setImageResource(orderItem.getImage());
name.setText(orderItem.getName());
place.setText(orderItem.getPlace());
price.setText(orderItem.getPrice());
holder.order_item_minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
value = value - 1;
holder.order_item_count.setText("" + value);
}
});
holder.order_item_plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
value = value + 1;
holder.order_item_count.setText("" + value);
}
});
}
@Override
public int getItemCount() {
return nList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView item_image;
TextView item_name, item_place, item_price,order_item_minus,order_item_count, order_item_plus;
public ViewHolder(@NonNull View itemView) {
super(itemView);
item_image = itemView.findViewById(R.id.order_item_image);
item_name = itemView.findViewById(R.id.order_item_name);
item_place = itemView.findViewById(R.id.order_item_place);
item_price = itemView.findViewById(R.id.order_item_price);
order_item_minus = itemView.findViewById(R.id.order_item_minus);
order_item_plus = itemView.findViewById(R.id.order_item_plus);
order_item_count = itemView.findViewById(R.id.order_item_count);
}
}
}
recyclerViewer.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
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="wrap_content"
android:layout_margin="10dp">
<LinearLayout
android:weightSum="10"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/order_item_image"
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="128dp"
android:scaleType="centerCrop"
android:src="@drawable/coke"/>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/order_item_name"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Name"/>
<TextView
android:id="@+id/order_item_place"
android:text="Description"
android:textSize="14sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView
android:id="@+id/order_item_price"
android:text="€ Preis"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#fff"
android:background="@drawable/capsule_order"
android:layout_height="30dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<TextView
android:id="@+id/order_item_minus"
android:layout_width="28dp"
android:layout_height="28dp"
android:textSize="25dp"
android:layout_gravity="center"
android:gravity="center"
android:background="@drawable/capsule_order"
android:textColor="#fff"
android:textStyle="bold"
android:text="-"/>
<TextView
android:id="@+id/order_item_count"
android:layout_width="28dp"
android:layout_height="28dp"
android:textSize="25dp"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#000"
android:textStyle="bold"
android:text="00"/>
<TextView
android:id="@+id/order_item_plus"
android:layout_width="28dp"
android:layout_height="28dp"
android:textSize="25dp"
android:layout_gravity="center"
android:gravity="center"
android:background="@drawable/capsule_order"
android:textColor="#fff"
android:textStyle="bold"
android:text="+"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
activity_order.xml
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview_order_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</androidx.recyclerview.widget.RecyclerView>
</androidx.core.widget.NestedScrollView>
答案1
得分: 1
因为您总是访问在适配器中初始化的 value 变量,
解决方案是将 value 添加到您的 ModelOrder 中,并在模型中进行初始化,
然后进行访问,
这样每一行都会有所不同,
OrderActivity.java // 用 counter = 0 初始化每个模型
orderArrayList.add(new ModelOrder(R.drawable.coke, "Coka Cola", "Kaltes Getränml", "6",0));
orderArrayList.add(new ModelOrder(R.drawable.fastfood, "Coka Cola", "Kaltes Getränml", "10",0));
orderArrayList.add(new ModelOrder(R.drawable.water, "Coka Cola", "Kaltes Getränml", "20",0));
orderArrayList.add(new ModelOrder(R.drawable.burger, "Coka Cola", "Kaltes Getränml", "30",0));
OrderAdapter.java
移除此行
// int value = 0; //Global
holder.order_item_minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
orderItem.setCounter(orderItem.getCounter()-1);
holder.order_item_count.setText("" + orderItem.getCounter());
}
});
holder.order_item_plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
orderItem.setCounter(orderItem.getCounter()+1);
holder.order_item_count.setText("" + orderItem.getCounter());
}
});
英文:
because you always access the value variable that you initialized in Adapter
solution is to
add value to your ModelOrder and initiate it in model
then access it
so .. it will be different for every row
OrderActivity.java // initialize every model with counter = 0
orderArrayList.add(new ModelOrder(R.drawable.coke, "Coka Cola", "Kaltes Getränml", "6",0));
orderArrayList.add(new ModelOrder(R.drawable.fastfood, "Coka Cola", "Kaltes Getränml", "10",0));
orderArrayList.add(new ModelOrder(R.drawable.water, "Coka Cola", "Kaltes Getränml", "20",0));
orderArrayList.add(new ModelOrder(R.drawable.burger, "Coka Cola", "Kaltes Getränml", "30",0));
OrderAdapter.java
remove this line
> int value = 0; //Global
holder.order_item_minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
orderItem.setCounter(orderItem.getCounter()-1);
holder.order_item_count.setText("" + orderItem.getCounter());
}
});
holder.order_item_plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
orderItem.setCounter(orderItem.getCounter()+1);
holder.order_item_count.setText("" + orderItem.getCounter());
}
});
答案2
得分: 0
你需要在你的 ModelOrder 中添加一个属性,名为 'quantity',其数据类型为整数(INT)。然后,根据加号和减号按钮的点击,对一个对象的 'quantity' 值进行增加/减少操作,并将 'quantity' 的值设置在你的计数文本视图中。
英文:
You've to add 'quantity' attribute which has INT data type in your ModelOrder
So, increment/decrement value of 'quantity' in an object according to plus/minus button click, and set 'quantity' value in your counter textview
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论