購物車(OK HTTP方法請求)

Castertonone發表於2018-07-01

一.http
1.HttpConfig
 
public class HttpConfig {
    public static String gouwuche_url = "https://www.zhaoapi.cn/product/getCarts";
}

二.model
1.GouWuCheListener
 
public interface GouWuCheListener {
    void getDataSuccess(String json);
    void getDataError(String error);
}

2.JiSuanListener
 
public interface JiSuanListener {
    void jiSuan(double price);
}
3.
IModel
 
public interface IModel {
    //獲取購物車資料
    void getGouWuCheData(String url, Map<String, String> map, GouWuCheListener gouWuCheListener);
    //計算總價
    void jisuan(List<GouWuBean.DataBean> list, JiSuanListener jiSuanListener);
}
4.
ModelImpl
 
public class ModelImpl implements IModel {
    private static final String TAG = "ModelImpl---";
    @Override
    public void getGouWuCheData(String url, Map<String, String> map, final GouWuCheListener gouWuCheListener) {
        HttpUtils httpUtils = HttpUtils.getHttpUtils();
        httpUtils.okPost(url, map);
        httpUtils.setOkLoadListener(new OkLoadListener() {
            @Override
            public void okLoadSuccess(String json) {
                Log.d(TAG, "okLoadSuccess: "+json);
                gouWuCheListener.getDataSuccess(json);
            }

            @Override
            public void okLoadError(String error) {
                Log.d(TAG, "okLoadError: "+error);
                gouWuCheListener.getDataError(error);
            }
        });
    }

    @Override
    public void jisuan(List<GouWuBean.DataBean> list, JiSuanListener jiSuanListener) {
        double sum = 0;
        for (int i = 0; i < list.size(); i++) {
            List<GouWuBean.ChildBean> list_child = list.get(i).getList();
            for (int j = 0; j < list_child.size(); j++) {
                boolean child_flag = list_child.get(j).isChild_flag();
                if (child_flag){
                    double price = list_child.get(j).getPrice();
                    sum+=price;
                }

            }
        }
        //記得回撥
        jiSuanListener.jiSuan(sum);
    }
}
三.presenter
1.
IPresenter
public interface IPresenter {
    void showGouWuCheToView(IModel iModel, IMainView iMainView);
    //將計算結果顯示到view
    void showSumToView(IModel iModel, IMainView iMainView);
}
2.
PresenterImpl
public class PresenterImpl implements IPresenter {
    private static final String TAG = "PresenterImpl----";

    //顯示資料
    @Override
    public void showGouWuCheToView(IModel iModel, final IMainView iMainView) {
        Map<String, String> map = new HashMap<>();
        map.put("uid", "71");
        iModel.getGouWuCheData(HttpConfig.gouwuche_url, map, new GouWuCheListener() {

            @Override
            public void getDataSuccess(String json) {
                Gson gson = new Gson();
                GouWuBean gouWuBean = gson.fromJson(json, GouWuBean.class);
                List<GouWuBean.DataBean> list = gouWuBean.getData();
                //傳入view
                iMainView.showGowWuList(list);
            }

            @Override
            public void getDataError(String error) {
                Log.d(TAG, "error: " + error);
            }
        });
    }
    //顯示計算結果
    @Override
    public void showSumToView(IModel iModel, final IMainView iMainView) {
        iModel.jisuan(iMainView.getList(), new JiSuanListener() {
            @Override
            public void jiSuan(double price) {
                iMainView.showSum(price);
            }
        });
    }
}

四.view
1.
IMainView

public interface IMainView {
    //顯示資料
    void showGowWuList(List<GouWuBean.DataBean> list);
    //顯示總價
    void showSum(double price);
    //獲取集合資料
    List<GouWuBean.DataBean> getList();
}

2.AddSubView

 
public class AddSubView extends LinearLayout implements View.OnClickListener {

    private TextView add;
    private TextView sub;
    private EditText count;

    public AddSubView(Context context) {
        this(context, null);
    }

    public AddSubView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AddSubView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = View.inflate(context, R.layout.add_layout, this);
        add = (TextView) view.findViewById(R.id.add);
        sub = (TextView) view.findViewById(R.id.sub);

        add.setOnClickListener(this);
        sub.setOnClickListener(this);
        count = (EditText) view.findViewById(R.id.count);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add:
                add();
                break;

            case R.id.sub:
                sub();
                break;
        }
    }

    //新增
    public void add() {
        String s = count.getText().toString();
        double i = Double.parseDouble(s);
        i++;
        count.setText(i + "");
    }

    //減少
    public void sub() {
        String s = count.getText().toString();
        double i = Double.parseDouble(s);
        if (i >= 1) {
            i--;
            count.setText(i + "");
        }

    }
    //設定數量

    //獲取數量
    public String getCount() {
        return count.getText().toString();
    }

    public void setCount(double count1){
        count.setText(count1+"");
    }

}
2.5     
add_layout
 
<LinearLayout 
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:background="@drawable/add_shape"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <TextView
        android:gravity="center"
        android:id="@+id/sub"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="@drawable/add_shape"
        android:text="-" />

    <EditText

        android:id="@+id/count"
        android:layout_width="50dp"
        android:layout_height="30dp"
        android:background="@null" />

    <TextView
        android:gravity="center"
        android:id="@+id/add"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="@drawable/add_shape"
        android:text="+" />
</LinearLayout>
3.MainActivity
 
public class MainActivity extends AppCompatActivity implements View.OnClickListener,IMainView {

    private ExpandableListView expandableListView;
    private CheckBox checkBox;
    private TextView tv_sum;
    private Button jisuan;
    private static final String TAG = "MainActivity0------";
    private List<GouWuBean.DataBean> list_tmp;
    private MyAdapter myAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化控制元件
        initViews();
        //請求資料
        initDatas();
    }

    private void initDatas() {
        PresenterImpl presenter = new PresenterImpl();
        presenter.showGouWuCheToView(new ModelImpl(),this);
    }

    private void initViews() {
        expandableListView = (ExpandableListView) findViewById(R.id.expandable_list_view);
        checkBox = (CheckBox) findViewById(R.id.cb);
        tv_sum = (TextView) findViewById(R.id.tv_sum);
        jisuan = (Button) findViewById(R.id.jisuan);

        checkBox.setOnClickListener(this);
        jisuan.setOnClickListener(this);
    }

    //點選事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //全選,全部選  總價
            case R.id.cb:
                boolean checked = checkBox.isChecked();
                for (int i = 0; i < list_tmp.size(); i++) {
                    list_tmp.get(i).setParent_flag(checked);
                    List<GouWuBean.ChildBean> list_child = list_tmp.get(i).getList();
                    for (int j = 0; j < list_child.size(); j++) {

                        list_child.get(j).setChild_flag(checked);
                    }
                }
                //重新整理介面卡
                myAdapter.notifyDataSetChanged();
                //呼叫計算總價的方法
                PresenterImpl presenter = new PresenterImpl();
                presenter.showSumToView(new ModelImpl(),this);
                break;

            case R.id.jisuan:
                Toast.makeText(MainActivity.this,"去計算總價", Toast.LENGTH_SHORT).show();
                break;
        }
    }
    //展示購物車資料
    @Override
    public void showGowWuList(List<GouWuBean.DataBean> list) {
        list_tmp=list;
        Log.d(TAG, "showGowWuList: "+list);

        myAdapter = new MyAdapter(MainActivity.this, list,this);
        expandableListView.setAdapter(myAdapter);
        //展開expandablelistview
        int childCount = expandableListView.getCount();
        for (int i = 0; i < childCount; i++) {
            expandableListView.expandGroup(i);
        }
    }
    //顯示總價
    @Override
    public void showSum(double price) {
        tv_sum.setText("合計:"+price);
        jisuan.setText("去結算("+price+")");
    }

    @Override
    public List<GouWuBean.DataBean> getList() {
        return list_tmp;
    }
}
3.5   
activity_main
<LinearLayout
    android:orientation="vertical"
     android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ExpandableListView
        android:id="@+id/expandable_list_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:groupIndicator="@null"
        android:layout_weight="1"></ExpandableListView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/cb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/tv_sum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="合計:0" />

        <Button
            android:id="@+id/jisuan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#F00"
            android:text="去結算(0)" />
    </LinearLayout>

</LinearLayout>
4.MyAdapter
 
public class MyAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<GouWuBean.DataBean> list;
    private IMainView iMainView;

    public MyAdapter(Context context, List<GouWuBean.DataBean> list, IMainView iMainView) {
        this.list = list;
        this.context = context;
        this.iMainView = iMainView;
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        return list.get(groupPosition).getList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).getList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        ParentViewHolder parentViewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_parent, null);
            CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.parent_cb);
            TextView textView = (TextView) convertView.findViewById(R.id.parent_title);
            parentViewHolder = new ParentViewHolder(checkBox, textView);

            convertView.setTag(parentViewHolder);
        } else {
            parentViewHolder = (ParentViewHolder) convertView.getTag();
        }
        //賦值
        parentViewHolder.getParent_tv().setText(list.get(groupPosition).getSellerName());
        parentViewHolder.getParent_cb().setChecked(list.get(groupPosition).isParent_flag());
        //單選框的點選事件
        final ParentViewHolder finalParentViewHolder = parentViewHolder;
        //點選事件
        parentViewHolder.getParent_cb().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = finalParentViewHolder.getParent_cb().isChecked();
                //改變bean裡面的值
                list.get(groupPosition).setParent_flag(isChecked);

                //給子列表賦值
                List<GouWuBean.ChildBean> list_child = MyAdapter.this.list.get(groupPosition).getList();

                for (int i = 0; i < list_child.size(); i++) {
                    GouWuBean.ChildBean childBean = list_child.get(i);
                    //改變bean裡面的值
                    childBean.setChild_flag(isChecked);
                }

                notifyDataSetChanged();

                //呼叫計算的方法
                PresenterImpl presenter = new PresenterImpl();
                presenter.showSumToView(new ModelImpl(), iMainView);

            }
        });

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_child, null);
            CheckBox child_cb = (CheckBox) convertView.findViewById(R.id.child_cb);
            TextView child_title = (TextView) convertView.findViewById(R.id.child_title);
            TextView dele = (TextView) convertView.findViewById(R.id.dele);
            ImageView child_pic = (ImageView) convertView.findViewById(R.id.child_pic);
            TextView tv_price = (TextView) convertView.findViewById(R.id.child_price);
            AddSubView addSubView = (AddSubView) convertView.findViewById(R.id.addSubView);

            childViewHolder = new ChildViewHolder(child_cb, child_pic, child_title, tv_price, addSubView, dele);

            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        //賦值
        final List<GouWuBean.ChildBean> child_list = this.list.get(groupPosition).getList();
        childViewHolder.getChild_title().setText(child_list.get(childPosition).getTitle());
        childViewHolder.getChild_price().setText(child_list.get(childPosition).getPrice() + "");
        //給單選框賦值
        childViewHolder.getChild_cb().setChecked(child_list.get(childPosition).isChild_flag());
        String images = child_list.get(childPosition).getImages();
        String pic_url = images.split("\\|")[0];

        Glide.with(context).load(pic_url).into(childViewHolder.getChild_pic());

        childViewHolder.getAddSubView().setCount(child_list.get(childPosition).getNum());

        //字條目單選框的點選事件
        final ChildViewHolder finalChildViewHolder = childViewHolder;
        childViewHolder.getChild_cb().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //獲取當前條目狀態
                boolean checked = finalChildViewHolder.getChild_cb().isChecked();
                //賦值--改變bean
                child_list.get(childPosition).setChild_flag(checked);
                //for迴圈,
                boolean flag = true;
                for (int i = 0; i < child_list.size(); i++) {
                    boolean child_flag = child_list.get(i).isChild_flag();
                    if (child_flag == false) {
                        flag = false;
                    }
                }
                //然後將flag賦值給父條目的bean
                list.get(groupPosition).setParent_flag(flag);

                notifyDataSetChanged();

                //呼叫計算的方法
                PresenterImpl presenter = new PresenterImpl();
                presenter.showSumToView(new ModelImpl(), iMainView);
            }
        });

        //刪除
        childViewHolder.getDelte().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                child_list.remove(childPosition);
                notifyDataSetChanged();
            }
        });

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    //父佈局的ViewHolder
    public class ParentViewHolder {
        private CheckBox parent_cb;
        private TextView parent_tv;

        public ParentViewHolder(CheckBox parent_cb, TextView parent_tv) {
            this.parent_cb = parent_cb;
            this.parent_tv = parent_tv;
        }

        public CheckBox getParent_cb() {
            return parent_cb;
        }

        public void setParent_cb(CheckBox parent_cb) {
            this.parent_cb = parent_cb;
        }

        public TextView getParent_tv() {
            return parent_tv;
        }

        public void setParent_tv(TextView parent_tv) {
            this.parent_tv = parent_tv;
        }
    }

    //子佈局的ViewHolder
    public class ChildViewHolder {
        private CheckBox child_cb;
        private ImageView child_pic;
        private TextView child_title;
        private TextView delte;
        private TextView child_price;
        private AddSubView addSubView;

        public ChildViewHolder(CheckBox child_cb, ImageView child_pic, TextView child_title, TextView child_price, AddSubView addSubView, TextView delte) {
            this.child_cb = child_cb;
            this.child_pic = child_pic;
            this.child_title = child_title;
            this.child_price = child_price;
            this.addSubView = addSubView;
            this.delte = delte;
        }

        public AddSubView getAddSubView() {
            return addSubView;
        }

        public TextView getDelte() {
            return delte;
        }

        public void setDelte(TextView delte) {
            this.delte = delte;
        }

        public void setAddSubView(AddSubView addSubView) {
            this.addSubView = addSubView;
        }

        public CheckBox getChild_cb() {
            return child_cb;
        }

        public void setChild_cb(CheckBox child_cb) {
            this.child_cb = child_cb;
        }

        public ImageView getChild_pic() {
            return child_pic;
        }

        public void setChild_pic(ImageView child_pic) {
            this.child_pic = child_pic;
        }

        public TextView getChild_title() {
            return child_title;
        }

        public void setChild_title(TextView child_title) {
            this.child_title = child_title;
        }

        public TextView getChild_price() {
            return child_price;
        }

        public void setChild_price(TextView child_price) {
            this.child_price = child_price;
        }
    }
}
4.5   
item_parent(父類)
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent" android:layout_height="match_parent"> <CheckBox android:id="@+id/parent_cb" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/parent_title" android:text="商家" android:singleLine="true" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
4.5   
item_child(子類)
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/child_cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/child_pic"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@mipmap/ic_launcher" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/child_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:text="商品名"
            android:textSize="25sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/child_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="商品價格"
                android:textColor="#F00" />

            <com.example.myceshi_0630.view.custumviews.AddSubView
                android:layout_width="100dp"
                android:id="@+id/addSubView"
                android:layout_height="30dp"></com.example.myceshi_0630.view.custumviews.AddSubView>
        </LinearLayout>

    </LinearLayout>
    <TextView
        android:id="@+id/dele"
        android:background="#F00"
        android:text="刪除"
        android:gravity="center"
        android:layout_width="50dp"
        android:layout_height="match_parent" />

</LinearLayout>
五。許可權
compile 'com.google.code.gson:gson:2.6.2'

compile 'com.squareup.okhttp3:okhttp:3.3.0'

compile 'com.github.bumptech.glide:glide:3.7.0'
<uses-permission android:name="android.permission.INTERNET"/>

相關文章