GoodsxiangqingActivity:
public class GoodsxiangqingActivity extends AppCompatActivity implements IGoodsView,IAddView {
private ImageView xiangqing_image;
private TextView xiangqing_price;
private TextView xiangqing_title;
private int uid=10863;
private GoodsPresenter goodsPresenter;
private AddGouwuchePresenter addGouwuchePresenter;
private int pid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goodsxiangqing);
xiangqing_image = (ImageView) findViewById(R.id.xiangqing_image);
xiangqing_price = (TextView) findViewById(R.id.xiangqing_price);
xiangqing_title = (TextView) findViewById(R.id.xiangqing_title);
final Intent intent = getIntent();
int pid = intent.getIntExtra("pid", 0);
goodsPresenter = new GoodsPresenter(this);
goodsPresenter.getGoods("https://www.zhaoapi.cn/product/getProductDetail",pid);
}
@Override
public void postGoods(final GoodsxiangqingBean goodsxiangqingBean) {
runOnUiThread(new Runnable() {
@Override
public void run() {
pid = goodsxiangqingBean.getData().getPid();
xiangqing_title.setText(goodsxiangqingBean.getData().getTitle());
xiangqing_price.setText(""+goodsxiangqingBean.getData().getPrice());
Glide.with(GoodsxiangqingActivity.this).load(goodsxiangqingBean.getData().getImages()).into(xiangqing_image);
xiangqing_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1=new Intent(GoodsxiangqingActivity.this,ZhaopianActivity.class);
intent1.putExtra("image",goodsxiangqingBean.getData().getImages());
startActivity(intent1);
}
});
}
});
}
//点击按钮返回
public void getfanhui(View view) {
finish();
}
//加入购物车
public void addCart(View view) {
addGouwuchePresenter = new AddGouwuchePresenter(this);
addGouwuchePresenter.getAdd("https://www.zhaoapi.cn/product/addCart",uid,pid);
}
@Override
public void postAdd(final AddGouwucheBean addGouwucheBean) {
runOnUiThread(new Runnable() {
@Override
public void run() {
int i= Integer.parseInt(addGouwucheBean.getCode());
if(i==0){
Toast.makeText(GoodsxiangqingActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(GoodsxiangqingActivity.this,"添加失败",Toast.LENGTH_SHORT).show();
}
}
});
}
}
GoodsxiangqingActivity布局:
<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.jingdong_xiangmu01.activity.GoodsxiangqingActivity"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_weight="1"
android:paddingTop="10dp"
android:orientation="horizontal">
<Button
android:onClick="getfanhui"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="9"
android:background="@drawable/leftjiantou" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_weight="1"
android:text="商品"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="9"
android:orientation="vertical">
<ImageView
android:id="@+id/xiangqing_image"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/xiangqing_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/xiangqing_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#f00" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"/>
<Button
android:onClick="addCart"
android:background="#f00"
android:text="加入购物车"
android:textColor="#fff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>
</LinearLayout>