新增購物車
阿新 • • 發佈:2018-11-19
1.佈局
<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"
tools:context="com.example.yuekaolx.ShopShow.ShopActivity"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:textSize="25sp"
android:gravity ="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="商品展示"
/>
</LinearLayout>
<ImageView
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:id="@+id/img"
android:layout_width ="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"
/>
<TextView
android:id="@+id/little"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="hahh1"
/>
<TextView
android:id="@+id/yjprice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#FF0000"
android:text="hahh1"
/>
<TextView
android:id="@+id/dzprice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="hahh1"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:onClick="gwcbtn"
android:gravity="center"
android:id="@+id/gwcbtn"
android:layout_width="0dp"
android:layout_height="50dp"
android:text="購物車"
android:layout_weight="1"
/>
<Button
android:onClick="jrgwc"
android:gravity="center"
android:id="@+id/addbtn"
android:layout_width="0dp"
android:layout_height="50dp"
android:text="加入購物車"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
2.MVp
2.1— ShowListSp
public interface ShowListSp {
void showSuccessListsDan(DanSpBean danSpBean);
void showFalidListsDan();
void addSuccessListsDan(String msg);
void addFalidListsDan();
}
2.2— ShowlistDanPresenter
public class ShowlistDanPresenter implements ShowListSp{
HashMap<String, String> hashMap = new HashMap<>();
private SpListmodel spListmodel;
private ShowSListPv showSListPv;
public ShowlistDanPresenter(ShowSListPv showSListPv, HashMap<String,String> hashMap) {
this.hashMap=hashMap;
this.showSListPv=showSListPv;
spListmodel = new SpListmodel();
}
public void pdModelList()
{
spListmodel.VereShowlist(this,hashMap.get("pid"));
}
public void pdModelAdd()
{
spListmodel.addShowlist(this,hashMap.get("pid"),hashMap.get("uid"));
}
@Override
public void showSuccessListsDan(DanSpBean danSpBean) {
showSListPv.showSuccessListDan(danSpBean);
}
@Override
public void showFalidListsDan() {
showSListPv.showFalidListDan();
}
@Override
public void addSuccessListsDan(String msg) {
showSListPv.addSuccessDan(msg);
}
@Override
public void addFalidListsDan() {
showSListPv.addFalidDan();
}
}
2.3– ShowSListPv
public interface ShowSListPv {
void showSuccessListDan(DanSpBean danSpBean);
void showFalidListDan();
void addSuccessDan(String msg);
void addFalidDan();
}
2.4– SpListmodel
public class SpListmodel {
public void VereShowlist(final ShowListSp showListSp,String pid)
{
HashMap<String,String> haspMap = new HashMap<>();
haspMap.put("pid",pid);
HttpUtils.getInstance().get(Constant.DGDJ, haspMap, new CallBack() {
@Override
public void OnSuccess(Object o) {
DanSpBean danSpBean= (DanSpBean) o;
Log.i("addaaa","----"+danSpBean.getCode());
showListSp.showSuccessListsDan(danSpBean);
}
@Override
public void OnFailed(Exception e) {
showListSp.showFalidListsDan();
}
},DanSpBean.class);
}
public void addShowlist(final ShowListSp showListSp,String pid,String uid)
{
HashMap<String,String> haspMap = new HashMap<>();
haspMap.put("pid",pid);
haspMap.put("uid",uid);
HttpUtils.getInstance().get(Constant.ADD, haspMap, new CallBack() {
@Override
public void OnSuccess(Object o) {
JgBean jgBean= (JgBean) o;
showListSp.addSuccessListsDan(jgBean.getMsg());
}
@Override
public void OnFailed(Exception e) {
showListSp.addFalidListsDan();
}
},JgBean.class);
}
}
2.5–ShopActivity
public class ShopActivity extends AppCompatActivity implements ShowSListPv {
@BindView(R.id.img)
ImageView img;
@BindView(R.id.yjprice)
TextView yjprice;
@BindView(R.id.dzprice)
TextView dzprice;
@BindView(R.id.gwcbtn)
Button gwcbtn;
@BindView(R.id.addbtn)
Button addbtn;
@BindView(R.id.little)
TextView little;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shop);
ButterKnife.bind(this);
HashMap<String, String> map = new HashMap<>();
map.put("pid", 1+"");
ShowlistDanPresenter showlistDanPresenter = new ShowlistDanPresenter(this, map);
showlistDanPresenter.pdModelList();
//加刪除線
yjprice.setPaintFlags(yjprice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
@Override
public void showSuccessListDan(DanSpBean danSpBean) {
DanSpBean.DataBean data = danSpBean.getData();
String[] split = data.getImages().split("\\|");
dzprice.setText("特價:" + data.getBargainPrice() + "");
yjprice.setText("原價:" + data.getPrice() + "");
little.setText(data.getTitle());
Glide.with(this).load(split[0]).into(img);
}
@Override
public void showFalidListDan() {
}
@Override
public void addSuccessDan(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
@Override
public void addFalidDan() {
}
//加入購物車
public void jrgwc(View view) {
HashMap<String, String> map = new HashMap<>();
map.put("pid", 1+"");
map.put("uid", 3416 + "");
ShowlistDanPresenter showlistDanPresenter = new ShowlistDanPresenter(this, map);
showlistDanPresenter.pdModelAdd();
}
// 跳轉購物車
public void gwcbtn(View view)
{
Intent intent = new Intent(this, GwcActivity.class);
startActivity(intent);
}
}
3.– bean
3.1– DanSpBean
public class DanSpBean {
/**
* msg :
* seller : {"description":"我是商家17","icon":"http://120.27.23.105/images/icon.png","name":"商家17","productNums":999,"score":5,"sellerid":17}
* code : 0
* data : {"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","itemtype":1,"pid":1,"price":118,"pscid":1,"salenum":0,"sellerid":17,"subhead":"每個中秋都不能簡單,無論身在何處,你總需要一塊餅讓生活更圓滿,京東月餅讓愛更圓滿京東自營,閃電配送,更多驚喜,快用手指戳一下","title":"北京稻香村 稻香村中秋節月餅 老北京月餅禮盒655g"}
*/
private String msg;
private SellerBean seller;
private String code;
private DataBean data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public SellerBean getSeller() {
return seller;
}
public void setSeller(SellerBean seller) {
this.seller = seller;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public DataBean getData() {
return data;
}
public void setData(DataBean data) {
this.data = data;
}
public static class SellerBean {
/**
* description : 我是商家17
* icon : http://120.27.23.105/images/icon.png
* name : 商家17
* productNums : 999
* score : 5.0
* sellerid : 17
*/
private String description;
private String icon;
private String name;
private int productNums;
private double score;
private int sellerid;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getProductNums() {
return productNums;
}
public void setProductNums(int productNums) {
this.productNums = productNums;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public int getSellerid() {
return sellerid;
}
public void setSellerid(int sellerid) {
this.sellerid = sellerid;
}
}
public static class DataBean {
/**
* bargainPrice : 111.99
* createtime : 2017-10-14T21:39:05
* detailUrl : https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends
* images : https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg
* itemtype : 1
* pid : 1
* price : 118.0
* pscid : 1
* salenum : 0
* sellerid : 17
* subhead : 每個中秋都不能簡單,無論身在何處,你總需要一塊餅讓生活更圓滿,京東月餅讓愛更圓滿京東自營,閃電配送,更多驚喜,快用手指戳一下
* title : 北京稻香村 稻香村中秋節月餅 老北京月餅禮盒655g
*/
private double bargainPrice;
private String createtime;
private String detailUrl;
private String images;
private int itemtype;
private int pid;
private double price;
private int pscid;
private int salenum;
private int sellerid;
private String subhead;
private String title;
public double getBargainPrice() {
return bargainPrice;
}
public void setBargainPrice(double bargainPrice) {
this.bargainPrice = bargainPrice;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getDetailUrl() {
return detailUrl;
}
public void setDetailUrl(String detailUrl) {
this.detailUrl = detailUrl;
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
public int getItemtype() {
return itemtype;
}
public void setItemtype(int itemtype) {
this.itemtype = itemtype;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getPscid() {
return pscid;
}
public void setPscid(int pscid) {
this.pscid = pscid;
}
public int getSalenum() {
return salenum;
}
public void setSalenum(int salenum) {
this.salenum = salenum;
}
public int getSellerid() {
return sellerid;
}
public void setSellerid(int sellerid) {
this.sellerid = sellerid;
}
public String getSubhead() {
return subhead;
}
public void setSubhead(String subhead) {
this.subhead = subhead;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
}
3.2– JgBean
public class JgBean {
/**
* msg : 加購成功
* code : 0
*/
private String msg;
private String code;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}