Я новичок в android, я создаю приложение для электронной коммерции, в котором пользователь, нажимающий кнопку add_to_cart, добавляет один товар в корзину, но когда я нажимаю кнопку просмотра корзины, ОДИН товар теперь добавляется в «МОЯ СТРАНИЦА КОРЗИНЫ» "позже я нажимаю кнопку" Назад "и снова нажимаю кнопку" Просмотр корзины ". Теперь доступны две одинаковые корзины для одного и того же продукта без нажатия кнопки add_to_cart
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"
android:gravity="center"
tools:context=".CartdetailsActivity">
<androidx.appcompat.widget.Toolbar
app:titleTextColor="@color/white"
android:id="@+id/cart_toolbar"
android:theme="@style/Action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"/>
<TextView
android:text="You have 2 product in cart"
android:textColor="@color/white"
android:gravity="center"
android:textSize="15sp"
android:background="@color/black"
android:id="@+id/cart_count"
android:layout_width="match_parent"
android:layout_height="30dp">
</TextView>
<ListView
android:id="@+id/cartlist"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
</ListView>
<Button
android:background="@drawable/button_background"
android:id="@+id/continue_to_shop"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
android:textSize="18sp"
android:layout_marginBottom="10dp"
android:text="Continue Shopping"/>
<LinearLayout
android:layout_gravity="center"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/totalprice"
android:text="BACK"
android:background="#89cff0"
android:gravity="center"
android:textSize="18sp"
android:layout_gravity="center"
android:layout_width="154dp"
android:textColor="@color/white"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_height="47dp">
</TextView>
<Button
android:id="@+id/proceed"
android:layout_width="155dp"
android:textSize="18sp"
android:textColor="@color/white"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="@color/colorPrimary"
android:text="Proceed">
</Button>
</LinearLayout>
</LinearLayout>
КОД
public class CartdetailsActivity extends AppCompatActivity {
public static ArrayList<CartItem> cartItems=cartItems=new ArrayList<CartItem>();
public CartsAdapter cartAdapter=new CartsAdapter(cartItems,this);
ListView addtocart;
String seller_nam,prodlabel,proimag;
int procost;
TextView spinner,totall,cartcount;
//Button deletebtn;
int val,prod_id;
int total=0;
Button checkout, continue_shopping,viewbtn,ok,cancel;
String cart_activity="cart";
int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cartdetails);
Toolbar toolbar = findViewById(R.id.cart_toolbar);
toolbar.setTitle("TRADEGLIMPSE");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
addtocart = (ListView) findViewById(R.id.cartlist);
// deletebtn=(Button)findViewById(R.id.delet);
checkout = (Button) findViewById(R.id.proceed);
//spinner=(TextView)findViewById(R.id.quantity);
cartcount=(TextView)findViewById(R.id.cart_count);
continue_shopping = (Button) findViewById(R.id.continue_to_shop);
totall = (TextView) findViewById(R.id.totalprice);
SharedPreferences pref1 = getApplicationContext().getSharedPreferences("sellername", 0);
if (pref1.getString("seller_name", " ") != " ") {
seller_nam = pref1.getString("seller_name", " ");
Log.wtf("prosellername", seller_nam);
}
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref5", 0);
prod_id = pref.getInt("productid", 0);
prodlabel = pref.getString("shop_name", " ");
procost = pref.getInt("cost", 0);
proimag = pref.getString("img", " ");
Log.wtf("prolabel......", prodlabel);
SharedPreferences pref2 = getApplicationContext().getSharedPreferences("valuee", 0);
if (pref2.getInt("val", 0) != 0) {
val = pref2.getInt("val", 0);
}
if (val == 1) {
checkout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(CartdetailsActivity.this, OrderNowActivity.class);
startActivity(i);
}
});
} else {
checkout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(CartdetailsActivity.this);
dialog.setContentView(R.layout.quantity_dialog);
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.setCancelable(false);
Button cancelbtn = dialog.findViewById(R.id.cancelbutton);
Button okbtn = dialog.findViewById(R.id.okbuton);
cancelbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
okbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(CartdetailsActivity.this, LoginpageActivity.class);
i.putExtra("cart", cart_activity);
startActivity(i);
}
});
dialog.show();
}
});
}
SharedPreferences pref6 = getApplicationContext().getSharedPreferences("review", 0);
SharedPreferences.Editor edit = pref6.edit();
edit.putInt("product_id",prod_id);
edit.putString("product_label", prodlabel);
edit.putInt("product_cost", procost);
edit.putString("product_imag", proimag);
edit.putString("seller_name", seller_nam);
edit.apply();
addtocart.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),"item clicked",Toast.LENGTH_LONG).show();
cartItems.remove(position);
cartAdapter.notifyDataSetChanged();
return true;
}
});
CartItem item = new CartItem(prodlabel, procost, proimag, seller_nam, prod_id);
cartItems.add(item);
cartAdapter.notifyDataSetChanged();
}
@Override
protected void onResume () {
super.onResume();
addtocart.setAdapter(cartAdapter);
cartAdapter.notifyDataSetChanged();
// String text= String.valueOf(cartAdapter.getCount());
//cartcount.setText("You have "+text+" product in cart");
}
}
Адаптер
public class CartsAdapter extends BaseAdapter {
public static ArrayList<CartItem> CartItems;
Context context;
increment increment;
LayoutInflater inflater;
int total;
public CartsAdapter(ArrayList<CartItem> cartItems, Context context) {
this.CartItems=cartItems;
this.context=context;
}
public CartsAdapter(increment increment) {
this.CartItems=cartItems;
this.increment=increment;
}
@Override
public int getCount() {
return CartItems.size();
}
@Override
public Object getItem(int position) {
return CartItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public interface increment {
void addcount(boolean flag);
}
private class ViewHolder{
TextView productname,shopname,price,totall;
ImageView image;
//Button deletebtn;
Spinner quant;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if(convertView==null){
holder=new ViewHolder();
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.singlecartitem, parent,false);
holder.productname=(TextView)convertView.findViewById(R.id.cart_dress);
holder.shopname=(TextView)convertView.findViewById(R.id.shopname);
holder.price=(TextView)convertView.findViewById(R.id.price);
// holder.deletebtn=(Button)convertView.findViewById(R.id.delet);
holder.quant=(Spinner) convertView.findViewById(R.id.quanti);
holder.image=(ImageView)convertView.findViewById(R.id.cart_image);
convertView.setTag(holder);
}else {
holder=(ViewHolder) convertView.getTag();
}
final CartItem row_pos=CartItems.get(position);
holder.productname.setText(row_pos.getProduct_name());
holder.shopname.setText(row_pos.getShop_name());
String imgg=row_pos.getProduct_img();
Picasso.with(context).load(imgg).into(holder.image);
return convertView;
}
}