This article shares the specific code of Android Studio to implement a simple shopping cart for your reference. The specific content is as follows
MainActivity layout file
<LinearLayout xmlns:andro xmlns:app="/apk/res-auto" xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android: android:layout_width="match_parent" android:layout_height="48dp" android:background="#E24146" android:orientation="vertical" > <TextView android: android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:minHeight="48dp" android:text="Shopping cart" android:textColor="#ffffff" android:textSize="17sp" /> </LinearLayout> <ListView android: android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:childIndicator="@null" android:groupIndicator="@null" > </ListView> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" > <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2.5" android:gravity="center_vertical" android:orientation="horizontal" > <CheckBox android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" android:layout_marginRight="4dp" android:checkMark="?android:attr/listChoiceIndicatorMultiple" android:gravity="center" android:minHeight="64dp" android:paddingLeft="10dp" android:textAppearance="?android:attr/textAppearanceLarge" android:visibility="visible" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:text="total:" android:textSize="16sp" android:textStyle="bold" /> <TextView android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="¥0.00" android:textSize="16sp" android:textStyle="bold" /> </LinearLayout> <TextView android: android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:clickable="true" android:background="#a29e9e" android:gravity="center" android:text="delete" android:textColor="#FAFAFA" /> <TextView android: android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="#E24146" android:clickable="true" android:gravity="center" android:text="Payment(0)" android:textColor="#FAFAFA" /> </LinearLayout> </LinearLayout>
Layout file for entry
<LinearLayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#CCCCCC" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <CheckBox android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" android:layout_marginRight="4dp" android:checkMark="?android:attr/listChoiceIndicatorMultiple" android:gravity="center" android:minHeight="64dp" android:minWidth="32dp" android:textAppearance="?android:attr/textAppearanceLarge" android:visibility="visible" /> <ImageView android: android:layout_width="85dp" android:layout_height="85dp" android:layout_marginBottom="15dp" android:layout_marginTop="13dp" android:scaleType="centerCrop" android:src="@mipmap/ic_launcher" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="center_vertical" android:layout_marginTop="10dp" android:layout_marginLeft="13dp" > <TextView android: android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:layout_marginTop="20dp" android:ellipsize="end" android:maxLines="2" android:text="commodity" android:textSize="14sp" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="30dp" android:orientation="horizontal" > <TextView android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:singleLine="true" android:textSize="14sp" android:textStyle="bold" android:text="price"/> <TextView android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_toRightOf="@+id/tv_goods_price" android:singleLine="true" android:textSize="10sp"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="15dp" android:orientation="horizontal" > <TextView android: android:layout_width="25dp" android:layout_height="25dp" android:gravity="center" android:background="#ccc" android:text="one" android:textSize="12sp" /> <TextView android: android:layout_width="25dp" android:layout_height="25dp" android:gravity="center" android:singleLine="true" android:text="1" android:textSize="12sp" /> <TextView android: android:layout_width="25dp" android:layout_height="25dp" android:gravity="center" android:text="ten" android:background="#ccc" android:textSize="12sp" /> </LinearLayout> </RelativeLayout> </RelativeLayout> </LinearLayout> </LinearLayout>
CartAdapter Adapter
public class CaetAdapter extends BaseAdapter { private Context context; private List<HashMap<String, String>> list; private HashMap<String, Integer> pitchOnMap; public HashMap<String, Integer> getPitchOnMap() { return pitchOnMap; } public void setPitchOnMap(HashMap<String, Integer> pitchOnMap) { = pitchOnMap; } public CaetAdapter(Context context, List<HashMap<String, String>> list) { = context; = list; pitchOnMap = new HashMap<>(); for (int i = 0; i < (); i++) { ((i).get("id"), 0); } } @Override public int getCount() { return (); } @Override public Object getItem(int position) { return (position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { convertView = (context, .item_layout, null); final CheckBox checkBox; ImageView icon; final TextView name, price, num, type, reduce, add; checkBox = (.check_box); icon = (.iv_adapter_list_pic); name = (.tv_goods_name); price = (.tv_goods_price); type = (.tv_type_size); num = (.tv_num); reduce = (.tv_reduce); add = (.tv_add); ((position).get("name")); ("¥ " + (((position).get("price"))) * (((position).get("count")))); ((position).get("type")); ((position).get("count")); if(((position).get("id"))== 0){ (false); }else{ (true); } (new () { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(()){ ((position).get("id"),1); }else{ ((position).get("id"), 0); } (pitchOnMap); } }); //The quantity of goods is reduced (new () { @Override public void onClick(View v) { if (((position).get("count")) <= 1) { (context, "The quantity cannot be reduced, it can only be deleted!", Toast.LENGTH_SHORT).show(); } else { (position).put("count", (((position).get("count")) - 1) + ""); notifyDataSetChanged(); } (pitchOnMap); } }); //Add quantity of goods (new () { @Override public void onClick(View v) { (position).put("count", (((position).get("count")) + 1) + ""); notifyDataSetChanged(); (pitchOnMap); } }); return convertView; } /** * Create an interface */ public interface RefreshPriceInterface { /** * Show the price to the total price * @param pitchOnMap */ void refreshPrice(HashMap<String, Integer> pitchOnMap); } /** * Define an interface object */ private RefreshPriceInterface mrefreshPriceInterface; /** * Expose a method to the outside * Show the price to the total price * @param refreshPriceInterface */ public void setRefreshPriceInterface(RefreshPriceInterface refreshPriceInterface) { mrefreshPriceInterface = refreshPriceInterface; } }
MainActivity
public class MainActivity extends AppCompatActivity implements ,{ private LinearLayout top_bar; private ListView listview; private CheckBox all_chekbox; private TextView price; private TextView delete; private TextView tv_go_to_pay; private List<User> goodsList; private UserDao userDao; private List<HashMap<String,String>> listmap=new ArrayList<>(); private CaetAdapter adapter; private double totalPrice = 0.00; private int totalCount = 0; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); initView(); } private void initView() { top_bar = (LinearLayout) findViewById(.top_bar); listview = (ListView) findViewById(); all_chekbox = (CheckBox) findViewById(.all_chekbox); price = (TextView) findViewById(.tv_total_price); delete = (TextView) findViewById(.tv_delete); tv_go_to_pay = (TextView) findViewById(.tv_go_to_pay); all_chekbox.setOnClickListener(this); (this); tv_go_to_pay.setOnClickListener(this); initDate(); adapter = new CaetAdapter(, listmap); (adapter); (this); } @Override public void onClick(View v) { switch (()) { case .all_chekbox: AllTheSelected(); break; case .tv_delete: checkDelete(()); break; case .tv_go_to_pay: if(totalCount<=0){ (this,"Please select the item to be paid for~",Toast.LENGTH_SHORT).show(); }else{ (this,"Payment successful",Toast.LENGTH_SHORT).show(); } break; } } /** * data */ private void initDate() { //Create a collection goodsList = new ArrayList<>(); //database userDao = ().getDaoSession().getUserDao(); (); //Data source for (int i = 0; i < 10; i++) { //Storing data to the database User user = new User((long) i, "The No. 1 in the Shopping Cart" + (i + 1) + "Product", (i + 20) + "code", "10", "10"); (user); } //Put data from the database into the collection goodsList=(); //Put the combined data into the HashMap collection for(int i=0;i<();i++){ HashMap<String,String> map=new HashMap<>(); ("id",(i).getId()+""); ("name",(i).getName()); ("type",((i).getType())); ("price",(i).getPrice()+""); ("count",(i).getCount()+""); (map); } } @Override public void refreshPrice(HashMap<String, Integer> pitchOnMap) { priceControl(pitchOnMap); } /** * Control price display total price */ private void priceControl(Map<String, Integer> pitchOnMap){ totalCount = 0; totalPrice = 0.00; for(int i=0;i<();i++){ if(((i).get("id"))==1){ totalCount=totalCount+((i).get("count")); double goodsPrice=((i).get("count"))*((i).get("price")); totalPrice=totalPrice+goodsPrice; } } (" ¥ "+totalPrice); tv_go_to_pay.setText("Payment("+totalCount+")"); } /** * Delete Control the total price of the price * @param map */ private void checkDelete(Map<String,Integer> map){ List<HashMap<String,String>> waitDeleteList=new ArrayList<>(); Map<String,Integer> waitDeleteMap =new HashMap<>(); for(int i=0;i<();i++){ if(((i).get("id"))==1){ ((i)); ((i).get("id"),((i).get("id"))); } } (waitDeleteList); (waitDeleteMap); priceControl(map); (); } /** *Select all or reverse */ private void AllTheSelected(){ HashMap<String,Integer> map=(); boolean isCheck=false; boolean isUnCheck=false; Iterator iter = ().iterator(); while (()) { entry = () (); if((().toString())==1){ isCheck=true; }else{ isUnCheck=true; } } if(isCheck==true&&isUnCheck==false){//I have selected all, do reverse selection for(int i=0;i<();i++){ ((i).get("id"),0); } all_chekbox.setChecked(false); }else if(isCheck==true && isUnCheck==true){//Selection of some, make all for(int i=0;i<();i++){ ((i).get("id"),1); } all_chekbox.setChecked(true); }else if(isCheck==false && isUnCheck==true){//No one is selected, choose all for(int i=0;i<();i++){ ((i).get("id"),1); } all_chekbox.setChecked(true); } priceControl(map); (map); (); } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.