DingDan创建与展示

本文详细介绍了如何创建和展示订单的过程,从视图层到模型层,涉及SuperClass的使用,以及Presenter和p接口在其中的角色。
摘要由CSDN通过智能技术生成

**************************创建**

******************VIEW***

package com.be.MyDingDan.view;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.be.MyDingDan.persenter.MyDingDanPersenter;

import bw.GetMyDingDan.view.view.GetMyDingDanActivity;
import view.bw.com.monthtext.R;

public class MyDingDanActivity extends AppCompatActivity implements IMyDingDanActivity{

    private MyDingDanPersenter myDingDanPersenter;
    private String uid;
    private double price;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_ding_dan);
        getSupportActionBar().hide();
        myDingDanPersenter = new MyDingDanPersenter(this);
        Intent intent = getIntent();
        uid = intent.getStringExtra("uid");
        price = intent.getDoubleExtra("price",0.00);
        myDingDanPersenter.getCJDD(uid,price+"");
    }

    @Override
    public void onSuccess(Object o) {
        String s = o.toString();
        if (s.equals("0")){
           //创建成功

            Toast.makeText(this,"创建订单成功",Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(MyDingDanActivity.this, GetMyDingDanActivity.class);
        startActivity(intent);
        finish();
        }
        //Toast.makeText(this,o.toString(),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFailed(String s) {
        Toast.makeText(this,s,Toast.LENGTH_SHORT).show();
    }
}

*****************VIEW接口***‘’‘’
package com.be.MyDingDan.view;

/**
 * Created by lenovo on 2018/1/17.
 */

public interface IMyDingDanActivity {
    void onSuccess(Object o);
    void onFailed(String s);
}


***********PERSENTER层

package com.be.MyDingDan.persenter;

import com.be.MyDingDan.model.MyDingDanModel;
import com.be.MyDingDan.view.IMyDingDanActivity;

/**
 * Created by lenovo on 2018/1/17.
 */

public class MyDingDanPersenter implements IMyDingDanPersenter {
    IMyDingDanActivity iMyDingDanActivity;
    MyDingDanModel myDingDanModel;
    public MyDingDanPersenter(IMyDingDanActivity iMyDingDanActivity) {
        this.iMyDingDanActivity = iMyDingDanActivity;
        myDingDanModel = new MyDingDanModel();
    }

    @Override
    public void getCJDD(String uid, String price) {
        myDingDanModel.getCJDD(uid,price,this);
    }

    @Override
    public void onSuccess(Object o) {
        if (iMyDingDanActivity!=null){
            iMyDingDanActivity.onSuccess(o.toString());
        }
    }

    @Override
    public void onFailed(String s) {
        if (iMyDingDanActivity!=null){
            iMyDingDanActivity.onFailed(s);
        }
    }

    @Override
    public void onDestory() {
        if (iMyDingDanActivity!=null){
            iMyDingDanActivity=null;
        }
    }
}

**************************Persenter接口***
package com.be.MyDingDan.persenter;

/**
 * Created by lenovo on 2018/1/17.
 */

public interface IMyDingDanPersenter {
    void getCJDD(String uid,String price);
    void onSuccess(Object o);
    void onFailed(String s);
    void onDestory();
}

***************************************MODEL************************


package com.be.MyDingDan.model;

import com.be.MyDingDan.bean.SupperDDBean;
import com.be.MyDingDan.persenter.IMyDingDanPersenter;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import view.bw.com.utils.OkHttpUtils;
import view.bw.com.utils.onFinish;

/**
 * Created by lenovo on 2018/1/17.
 */

public class MyDingDanModel implements IMyDingDanModel{
    @Override
    public void getCJDD(String uid, String price, final IMyDingDanPersenter iMyDingDanPersenter) {
        String url = "https://www.zhaoapi.cn/product/createOrder";
        OkHttpUtils utils = new OkHttpUtils();
        Map<String, String> map = new HashMap<>();
        map.put("uid",uid);
        map.put("price",price);
        utils.doPost(url, map, new onFinish() {
            @Override
            public void onSuccess(Object o) {
                String s = o.toString();
                Gson g = new Gson();
                SupperDDBean supperDDBean = g.fromJson(s, SupperDDBean.class);
                String code = supperDDBean.getCode();
                iMyDingDanPersenter.onSuccess(code);
            }

            @Override
            public void onFailed(String s) {
                iMyDingDanPersenter.onFailed(s);
            }
        });

    }
}
*******************************MODEL接口

package com.be.MyDingDan.model;

import com.be.MyDingDan.persenter.IMyDingDanPersenter;

/**
 * Created by lenovo on 2018/1/17.
 */

public interface IMyDingDanModel {
    void getCJDD(String uid, String price, IMyDingDanPersenter iMyDingDanPersenter);
}


**************************************SupperClass*********************************

package com.be.MyDingDan.bean;

/**
 * Created by lenovo on 2018/1/17.
 */

public class SupperDDBean {

    /**
     * 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;
    }
}


**********************************************展示订单***********************************


*********************************************VIEW****************************9

package bw.GetMyDingDan.view.view;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

import bw.GetMyDingDan.view.adapter.MyRvAdapter;
import bw.GetMyDingDan.view.bean.SupperGetBean;
import bw.GetMyDingDan.view.persenter.GetMyDingDanPersenter;
import view.bw.com.monthtext.R;

public class GetMyDingDanActivity extends AppCompatActivity implements IGetMyDingDanActivity {
    private int curr;
    private XRecyclerView mMyRecycler;
    private GetMyDingDanPersenter getMyDingDanPersenter;
    private List<SupperGetBean.DataBean> list;
    private MyRvAdapter adapter;
    private TabLayout mTablayout;
    private String[] tabs = {"已支付", "待支付", "已取消"};
    private ViewPager mVp;
    private TabLayout mTabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_my_ding_dan);
        initView();
        getMyDingDanPersenter = new GetMyDingDanPersenter(this);
        getMyDingDanPersenter.getDingDan("4582");

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mMyRecycler.setLayoutManager(layoutManager);
        getSupportActionBar().hide();


        mMyRecycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            //下拉刷新
            public void onRefresh() {
                mMyRecycler.refreshComplete();
                Toast.makeText(GetMyDingDanActivity.this, "刷新成功", Toast.LENGTH_SHORT).show();

            }

            @Override
            //上拉加载
            public void onLoadMore() {
                mMyRecycler.refreshComplete();
                Toast.makeText(GetMyDingDanActivity.this, "加载成功", Toast.LENGTH_SHORT).show();
            }
        });

        //第一次获取数据

        mTabLayout.addTab(mTabLayout.newTab().setText("待支付"));
        mTabLayout.addTab(mTabLayout.newTab().setText("已支付"));
        mTabLayout.addTab(mTabLayout.newTab().setText("已取消"));
    }


    @Override
    public void onSuccess(List<SupperGetBean.DataBean> list) {
        this.list = list;
        adapter = new MyRvAdapter(list, GetMyDingDanActivity.this);
        mMyRecycler.setAdapter(adapter);
    }

    @Override
    public void onFailed(String s) {
        Toast.makeText(GetMyDingDanActivity.this, s, Toast.LENGTH_SHORT).show();
    }

    private void initView() {
        mMyRecycler = (XRecyclerView) findViewById(R.id.myRecycler);
        mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
    }
}
************************************view接口********************************

package bw.GetMyDingDan.view.view;

import java.util.List;

import bw.GetMyDingDan.view.bean.SupperGetBean;

/**
 * Created by lenovo on 2018/1/17.
 */

public interface IGetMyDingDanActivity {
    void onSuccess(List<SupperGetBean.DataBean> list);
    void onFailed(String s);
}

******************************************PERSENTER*****************************

package bw.GetMyDingDan.view.persenter;

import java.util.List;

import bw.GetMyDingDan.view.bean.SupperGetBean;
import bw.GetMyDingDan.view.model.GetMyDingDanModel;
import bw.GetMyDingDan.view.view.IGetMyDingDanActivity;

/**
 * Created by lenovo on 2018/1/17.
 */

public class GetMyDingDanPersenter implements IGetMyDingDanPersenter {

    IGetMyDingDanActivity iGetMyDingDanActivity;
    GetMyDingDanModel getMyDingDanModel;
    public GetMyDingDanPersenter(IGetMyDingDanActivity iGetMyDingDanActivity) {
        this.iGetMyDingDanActivity = iGetMyDingDanActivity;
        getMyDingDanModel = new GetMyDingDanModel();
    }

    @Override
    public void getDingDan(String uid) {
        getMyDingDanModel.getDingDan(uid,this);
    }

    @Override
    public void onSueecss(List<SupperGetBean.DataBean> list) {
        if (iGetMyDingDanActivity!=null){
            iGetMyDingDanActivity.onSuccess(list);
        }
    }

    @Override
    public void onFailed(String s) {
        if (iGetMyDingDanActivity!=null){
            iGetMyDingDanActivity.onFailed(s);
        }
    }

    @Override
    public void onDestory() {
        if (iGetMyDingDanActivity!=null){
            iGetMyDingDanActivity=null;
        }
    }
}


********************************p接口

package bw.GetMyDingDan.view.persenter;

import java.util.List;

import bw.GetMyDingDan.view.bean.SupperGetBean;

/**
 * Created by lenovo on 2018/1/17.
 */

public interface IGetMyDingDanPersenter {
    void getDingDan(String uid);
    void onSueecss(List<SupperGetBean.DataBean> list);
    void onFailed(String s);
    void onDestory();
}


*************************************MODEL

package bw.GetMyDingDan.view.model;

import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import bw.GetMyDingDan.view.bean.SupperGetBean;
import bw.GetMyDingDan.view.persenter.IGetMyDingDanPersenter;
import view.bw.com.utils.OkHttpUtils;
import view.bw.com.utils.onFinish;

/**
 * Created by lenovo on 2018/1/17.
 */

public class GetMyDingDanModel implements IGetMyDingDanModel {
    @Override
    public void getDingDan(String uid, final IGetMyDingDanPersenter iGetMyDingDanPersenter) {
        String url = "https://www.zhaoapi.cn/product/getOrders";
        OkHttpUtils utils = new OkHttpUtils();
        Map<String, String> map = new HashMap<>();
        map.put("uid",uid);
        utils.doPost(url, map, new onFinish() {
            @Override
            public void onSuccess(Object o) {
                String result = o.toString();
                Gson gson = new Gson();
                SupperGetBean supperGetBean = gson.fromJson(result, SupperGetBean.class);
                List<SupperGetBean.DataBean> data = supperGetBean.getData();
                iGetMyDingDanPersenter.onSueecss(data);
            }

            @Override
            public void onFailed(String s) {
                iGetMyDingDanPersenter.onFailed(s);
            }
        });
    }
}
*************************model接口

package bw.GetMyDingDan.view.model;

import bw.GetMyDingDan.view.persenter.IGetMyDingDanPersenter;

/**
 * Created by lenovo on 2018/1/17.
 */

public interface IGetMyDingDanModel {
    void getDingDan(String uid, IGetMyDingDanPersenter iGetMyDingDanPersenter);
}

***************************SupperClass

package bw.GetMyDingDan.view.bean;

import java.util.List;

/**
 * Created by lenovo on 2018/1/17.
 */

public class SupperGetBean {

    /**
     * msg : 请求成功
     * code : 0
     * data : [{"createtime":"2017-10-19T20:28:43","orderid":20,"price":100,"status":0,"title":"订单标题测试3","uid":71},{"createtime":"2017-10-19T20:44:40","orderid":31,"price":11800,"status":0,"title":"订单标题测试14","uid":71},{"createtime":"2017-10-19T20:44:51","orderid":32,"price":11800,"status":2,"title":"订单标题测试15","uid":71},{"createtime":"2017-10-20T08:02:07","orderid":43,"price":11800,"status":0,"title":"订单标题测试","uid":71},{"createtime":"2017-10-20T08:02:16","orderid":44,"price":11800,"status":2,"title":"订单标题测试","uid":71},{"createtime":"2017-10-22T15:14:39","orderid":890,"price":11800,"status":2,"title":"","uid":71},{"createtime":"2017-11-09T09:17:20","orderid":1446,"price":99.99,"status":1,"title":"订单标题测试","uid":71},{"createtime":"2017-11-09T09:20:58","orderid":1447,"price":567,"status":2,"title":"订单标题测试","uid":71},{"createtime":"2017-11-09T09:20:58","orderid":1448,"price":256.99,"status":2,"title":"订单标题测试","uid":71},{"createtime":"2017-11-09T09:20:58","orderid":1449,"price":399,"status":2,"title":"订单标题测试","uid":71}]
     * page : 1
     */

    private String msg;
    private String code;
    private String page;
    private List<DataBean> data;

    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;
    }

    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

    public List<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    public static class DataBean {
        /**
         * createtime : 2017-10-19T20:28:43
         * orderid : 20
         * price : 100.0
         * status : 0
         * title : 订单标题测试3
         * uid : 71
         */

        private String createtime;
        private String orderid;
        private String price;
        private String status;
        private String title;
        private String uid;

        public String getCreatetime() {
            return createtime;
        }

        public void setCreatetime(String createtime) {
            this.createtime = createtime;
        }

        public String getOrderid() {
            return orderid;
        }

        public void setOrderid(String orderid) {
            this.orderid = orderid;
        }

        public String getPrice() {
            return price;
        }

        public void setPrice(String price) {
            this.price = price;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getUid() {
            return uid;
        }

        public void setUid(String uid) {
            this.uid = uid;
        }
    }
}
*******************************适配器

package bw.GetMyDingDan.view.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import bw.GetMyDingDan.view.bean.SupperGetBean;
import view.bw.com.monthtext.R;

/**
 * Created by lenovo on 2018/1/17.
 */

public class MyRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<SupperGetBean.DataBean> list;
    private Context context;
    private SupperGetBean.DataBean dataBean;
    private String status;

    public MyRvAdapter(List<SupperGetBean.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.line_item_layout, parent, false);
        HoldView holdView = new HoldView(view);
        return holdView;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        dataBean = list.get(position);
        ((HoldView) holder).text_title.setText(dataBean.getTitle());
        ((HoldView) holder).text_dingdan.setText("订单ID:"+ dataBean.getOrderid());
        ((HoldView) holder).text_price.setText(""+ dataBean.getPrice());
        status = dataBean.getStatus();
        if (status.equals("0")){
            ((HoldView) holder).text_status.setText("订单状态:待支付");
        }else if (status.equals("1")){
            ((HoldView) holder).text_status.setText("订单状态:已支付");
        }else if (status.equals("2")){
            ((HoldView) holder).text_status.setText("订单状态:已取消");
        }else{
            ((HoldView) holder).text_status.setText("订单状态:");
        }
        ((HoldView) holder).creatTime.setText("创建时间:"+ dataBean.getCreatetime());
        ((HoldView) holder).submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Toast.makeText(context,"AAA",Toast.LENGTH_SHORT).show();
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("确定提交订单?");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        list.get(position).setStatus("1");
                        notifyDataSetChanged();
                    }
                });

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        list.get(position).setStatus("2");
                        notifyDataSetChanged();
                    }
                });
                builder.show();
            }
        });
    }

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

    private class HoldView extends RecyclerView.ViewHolder {
        private TextView text_title;
        private final TextView creatTime;
        private final TextView text_dingdan;
        private final TextView text_price;
        private final TextView text_status;
        private final Button submit;

        public HoldView(View itemView) {
            super(itemView);
            //根据onCreateViewHolderHoldView所添加的xml布局找到空间
            text_title = (TextView) itemView.findViewById(R.id.text_title);
            creatTime = itemView.findViewById(R.id.text_createtime);
            text_dingdan = itemView.findViewById(R.id.text_dingdan);
            text_price = itemView.findViewById(R.id.text_price);
            text_status = itemView.findViewById(R.id.text_status);
            submit = itemView.findViewById(R.id.btn_submit);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值