購物提交

public static final String BASE_URL="http://mobile.bwstudent.com/";

主佈局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".DingDanActivity">


    <android.support.v7.widget.CardView
        android:id="@+id/no_shop_address_error"
        android:layout_margin="@dimen/dp_10"
        android:layout_width="match_parent"
        app:cardCornerRadius="@dimen/dp_10"
        android:layout_height="wrap_content">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_50">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:text="暫無收穫地址"/>

        </RelativeLayout>
    </android.support.v7.widget.CardView>
    <android.support.v7.widget.CardView
        android:id="@+id/shop_address_receive_card"
        app:cardCornerRadius="@dimen/dp_10"
        android:layout_width="match_parent"
        android:layout_marginTop="@dimen/dp_10"
        android:layout_marginRight="@dimen/dp_10"
        android:layout_marginLeft="@dimen/dp_10"
        android:layout_height="@dimen/dp_60">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:id="@+id/set_shop_address_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="@dimen/dp_10"
                android:text="名字"/>
            <TextView
                android:id="@+id/set_shop_address_phone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:padding="@dimen/dp_10"
                android:text="13334445565656"/>
            <TextView
                android:id="@+id/set_shop_address_address"

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/set_shop_address_name"
                android:text="誰知道你是哪的"/>

        </RelativeLayout>

    </android.support.v7.widget.CardView>
    <android.support.v7.widget.RecyclerView
        android:layout_margin="@dimen/dp_10"
        android:id="@+id/commit_order_recycle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/dp_10"
        android:layout_below="@+id/shop_address_receive_card">

    </android.support.v7.widget.RecyclerView>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/pup_recy"
        android:background="@drawable/gray_item"
        android:layout_marginLeft="@dimen/dp_10"
        android:layout_marginRight="@dimen/dp_10"
        android:layout_below="@+id/shop_address_receive_card"
        android:layout_width="match_parent"
        android:visibility="gone"
        android:layout_height="@dimen/dp_200">

    </android.support.v7.widget.RecyclerView>

    <LinearLayout
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_45"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/commit_order_bottom_text"
            android:layout_width="@dimen/dp_0"
            android:layout_height="match_parent"
            android:layout_weight="7"
            android:background="#c175a9"
            android:gravity="center"
            android:text="共多少件商品,共多少錢"
            android:textColor="#e0cc2872" />

        <Button
            android:id="@+id/commit_order_bottom_btn"
            android:layout_width="@dimen/dp_0"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:background="#e0cc2872"
            android:gravity="center"
            android:text="提交訂單"
            android:textColor="#d1c0c0"
            android:textSize="@dimen/sp_16" />
    </LinearLayout>

    </RelativeLayout>

adapter佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:padding="@dimen/dp_10"
        android:layout_height="wrap_content">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/item_shape"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/shopcar_item_image"
            android:layout_width="@dimen/dp_85"
            android:padding="5dp"
            android:layout_height="@dimen/dp_85" />
        <TextView
            android:id="@+id/shopcar_item_name"
            android:padding="@dimen/dp_5"
            android:layout_toRightOf="@id/shopcar_item_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="商品名稱"/>
        <TextView
            android:id="@+id/shopcar_item_price"
            android:padding="@dimen/dp_5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#f00"
            android:layout_toRightOf="@id/shopcar_item_image"
            android:layout_alignBottom="@id/shopcar_item_image"
            android:text="¥1223"/>
        <TextView
            android:id="@+id/shopcar_item_num"
            android:padding="@dimen/dp_5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignBottom="@id/shopcar_item_image"
            android:text="×3"/>

    </RelativeLayout>
    </RelativeLayout>
</LinearLayout>

bean類

package com.example.activity.myapplication.bean;

import java.io.Serializable;
import java.util.List;

public class ShopData implements Serializable {

    List<ChaShoppData.ResultBean> result;

    public ShopData() {
    }

    public void setResult(List<ChaShoppData.ResultBean> result) {
        this.result = result;
    }

    public ShopData(List<ChaShoppData.ResultBean> result) {
        this.result = result;
    }

    public List<ChaShoppData.ResultBean> getResult() {
        return result;
    }
}

代碼

package com.example.activity.myapplication;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.activity.myapplication.adapter.MoAddressAdapter;
import com.example.activity.myapplication.adapter.OrderShopCarAdapter;

import com.example.activity.myapplication.bean.AddressData;
import com.example.activity.myapplication.bean.ChaShoppData;
import com.example.activity.myapplication.bean.ShopData;
import com.example.activity.myapplication.presenter.PersenterIpml;
import com.example.activity.myapplication.utils.Contacts;
import com.example.activity.myapplication.view.IView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DingDanActivity extends AppCompatActivity implements IView, View.OnClickListener {


    private CardView no_shop_address_error;
    private TextView set_shop_address_name;
    private TextView set_shop_address_phone;
    private TextView set_shop_address_address;
    private CardView shop_address_receive_card;
    private RecyclerView commit_order_recycle;
    private RecyclerView pup_recy;
    private TextView commit_order_bottom_text;
    private Button commit_order_bottom_btn;
    private List<ChaShoppData.ResultBean> result;
    private ArrayList<AddressData.ResultBean>aList=new ArrayList<>();
    private MoAddressAdapter moAddressAdapter;
    boolean isHide=true;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ding_dan);
        initView();
        //接收傳值
        Intent intent=getIntent();
        ShopData shopData = (ShopData) intent.getSerializableExtra("checkedshop");
        result = shopData.getResult();

        moAddressAdapter = new MoAddressAdapter(aList,this);
        pup_recy.setLayoutManager(new LinearLayoutManager(this));
        pup_recy.setAdapter(moAddressAdapter);
        moAddressAdapter.setPopRecyAdapterListener(new MoAddressAdapter.PopRecyAdapterListener() {
            @Override
            public void onItemClick(int i) {
                set_shop_address_name.setText(aList.get(i).getRealName());
                set_shop_address_phone.setText(aList.get(i).getPhone()+"");
                set_shop_address_address.setText(aList.get(i).getAddress());
                pup_recy.setVisibility(View.GONE);
                //Toast.makeText(getApplicationContext(),aList.toString()+"",Toast.LENGTH_SHORT).show();

            }
        });

        commit_order_recycle.setLayoutManager(new LinearLayoutManager(this));
        OrderShopCarAdapter adapter=new OrderShopCarAdapter(getApplicationContext(),result);
        commit_order_recycle.setAdapter(adapter);
        initData();

    }
    private void initData() {
        double price=0;
        int count=0;
        for (int i = 0; i < result.size(); i++) {
            price+=result.get(i).getPrice()*result.get(i).getCount();
            count+=result.get(i).getCount();
        }
        commit_order_bottom_text.setText("共"+count+"件商品"+",供需付款"+price+"元");



    }
    @Override
    protected void onResume() {
        super.onResume();
        setUrlData();
    }

    private void setUrlData() {
        PersenterIpml presenter = new PersenterIpml(this);
        HashMap<String, String> map = new HashMap<>();
        HashMap<String,Object> hashMap=new HashMap<>();
        sp = getSharedPreferences("long",MODE_PRIVATE);
        int userId = sp.getInt("userId", 1);
        String sessionId = sp.getString("sessionId", "");
        hashMap.put("userId", userId);
        hashMap.put("sessionId", sessionId);
        presenter.requestGEt(Contacts.TONGSHOPPING_URL, map, hashMap, AddressData.class);

    }

    @Override
    public void success(Object data) {

        if(data instanceof AddressData){
            AddressData addressData= (AddressData) data;
            aList.clear();
            aList.addAll(addressData.getResult());
            moAddressAdapter.notifyDataSetChanged();
            if(addressData.getResult().size()==0){
                no_shop_address_error.setVisibility(View.VISIBLE);
                shop_address_receive_card.setVisibility(View.GONE);
            }else{
                no_shop_address_error.setVisibility(View.GONE);
                shop_address_receive_card.setVisibility(View.VISIBLE);
                for (int i = 0; i <addressData.getResult().size() ; i++) {
                    if(addressData.getResult().get(i).getWhetherDefault()==1){
                        set_shop_address_name.setText(addressData.getResult().get(i).getRealName());
                        set_shop_address_phone.setText(addressData.getResult().get(i).getPhone()+"");
                        set_shop_address_address.setText(addressData.getResult().get(i).getAddress());
                    }
                }
            }
        }
    }

    @Override
    public void error(Object error) {

    }

    private void initView() {
        no_shop_address_error = (CardView) findViewById(R.id.no_shop_address_error);
        set_shop_address_name = (TextView) findViewById(R.id.set_shop_address_name);
        set_shop_address_phone = (TextView) findViewById(R.id.set_shop_address_phone);
        set_shop_address_address = (TextView) findViewById(R.id.set_shop_address_address);
        shop_address_receive_card = (CardView) findViewById(R.id.shop_address_receive_card);
        commit_order_recycle = (RecyclerView) findViewById(R.id.commit_order_recycle);
        pup_recy = (RecyclerView) findViewById(R.id.pup_recy);
        commit_order_bottom_text = (TextView) findViewById(R.id.commit_order_bottom_text);
        commit_order_bottom_btn = (Button) findViewById(R.id.commit_order_bottom_btn);

        commit_order_bottom_btn.setOnClickListener(this);
        shop_address_receive_card.setOnClickListener(this);
        no_shop_address_error.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.commit_order_bottom_btn:


                break;

            case R.id.shop_address_receive_card:
                if(isHide){
                    pup_recy.setVisibility(View.VISIBLE);
                    isHide=false;
                    Toast.makeText(getApplicationContext(),aList.toString()+"",Toast.LENGTH_SHORT).show();
                }else {
                    pup_recy.setVisibility(View.GONE);
                    isHide=true;
                }


                break;
            case R.id.no_shop_address_error:
                startActivity(new Intent(this,AddressActivity.class));
                break;
        }
    }

}

package com.example.activity.myapplication.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.activity.myapplication.R;
import com.example.activity.myapplication.bean.ChaShoppData;

import java.util.List;

public class OrderShopCarAdapter extends RecyclerView.Adapter<OrderShopCarAdapter.ViewHolder> {
    Context context;
    List<ChaShoppData.ResultBean> list;

    public OrderShopCarAdapter(Context context, List<ChaShoppData.ResultBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view=View.inflate(context, R.layout.shop_car_order_item,null);
        ViewHolder holder=new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        Glide.with(context).load(list.get(i).getPic()).into(viewHolder.image);
        viewHolder.name.setText(list.get(i).getCommodityName());
        viewHolder.price.setText(list.get(i).getPrice()+"");
        viewHolder.num.setText("×"+list.get(i).getCount());

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView image;
        TextView name,price,num;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            image= itemView.findViewById(R.id.shopcar_item_image);
            price=itemView.findViewById(R.id.shopcar_item_price);
            name= itemView.findViewById(R.id.shopcar_item_name);
            num=itemView.findViewById(R.id.shopcar_item_num);

        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章