andriod——订单查询


adapter——MyAdapter

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.week3.R;
import com.example.week3.bean.OrderBean;
import com.example.week3.presenter.CancleOrderPresenter;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<OrderBean.DataBean> list;
    private final CancleOrderPresenter cancleOrderPresenter;

    public MyAdapter(Context context, List<OrderBean.DataBean> list) {
        this.context = context;
        this.list = list;
        cancleOrderPresenter = new CancleOrderPresenter();
    }

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

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final OrderBean.DataBean dataBean = list.get(position);
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        myViewHolder.tv_title.setText(dataBean.getTitle());
        int status = dataBean.getStatus();
        if (status == 0) {
            myViewHolder.tv_status.setText("待支付");
            myViewHolder.tv.setText("取消订单");
            myViewHolder.tv_status.setTextColor(Color.RED);
        } else if (status == 1) {
            myViewHolder.tv_status.setText("已支付");
            myViewHolder.tv.setText("查看订单");
            myViewHolder.tv_status.setTextColor(Color.BLACK);
        } else {
            myViewHolder.tv_status.setText("已取消");
            myViewHolder.tv.setText("查看订单");
            myViewHolder.tv_status.setTextColor(Color.BLACK);
        }

        myViewHolder.tv_price.setText("价格:" + dataBean.getPrice());
        myViewHolder.tv_time.setText("创建时间:" + dataBean.getCreatetime());

        myViewHolder.tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(context)
                        .setTitle("提示")
                        .setMessage("确定取消订单吗?")
                        .setPositiveButton("是", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                cancleOrderPresenter.cancleOrder("1", dataBean.getOrderid() + "");
                            }
                        })
                        .setNegativeButton("否", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .show();
            }
        });


    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {

        private final TextView tv_title;
        private final TextView tv_status;
        private final TextView tv_price;
        private final TextView tv_time;
        private final TextView tv;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_status = itemView.findViewById(R.id.tv_status);
            tv_price = itemView.findViewById(R.id.tv_price);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv = itemView.findViewById(R.id.tv);
        }
    }
}

bean—— OrderBean
public class OrderBean {

    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 : 1
         * title : 订单标题测试3
         * uid : 71
         */

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

        public String getCreatetime() {
            return createtime;
        }

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

        public int getOrderid() {
            return orderid;
        }

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

        public double getPrice() {
            return price;
        }

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

        public int getStatus() {
            return status;
        }

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

        public String getTitle() {
            return title;
        }

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

        public int getUid() {
            return uid;
        }

        public void setUid(int uid) {
            this.uid = uid;
        }
    }
}

model—— CancleOrderModel
import com.example.week3.net.Api;
import com.example.week3.net.HttpUtils;
import com.example.week3.net.OnNetListener;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class CancleOrderModel {

    public void cancleOrder(String status, String orderId, OnNetListener onNetListener) {
        Map<String, String> params = new HashMap<>();
        params.put("uid", "71");
        params.put("orderId", orderId);
        params.put("status", status);
        HttpUtils.getHttpUtils().doPost(Api.CANCLE_ORDER, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });
    }
}
model—— IOrderModel
import com.example.week3.bean.OrderBean;
import com.example.week3.net.OnNetListener;


public interface IOrderModel {
    public void getOrder(String status, String page, OnNetListener<OrderBean> onNetListener);
}
model—— OrderModel
import android.os.Handler;
import android.os.Looper;

import com.example.week3.bean.OrderBean;
import com.example.week3.net.Api;
import com.example.week3.net.HttpUtils;
import com.example.week3.net.OnNetListener;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class OrderModel implements IOrderModel {
    private Handler handler = new Handler(Looper.getMainLooper());

    @Override
    public void getOrder(final String status, String page, final OnNetListener<OrderBean> onNetListener) {
        Map<String, String> params = new HashMap<>();
        params.put("uid", "71");
        params.put("status", status);
        params.put("page", page);
        HttpUtils.getHttpUtils().doPost(Api.ORDER, params, new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onNetListener.onFailure(e);
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                final OrderBean orderBean = new Gson().fromJson(string, OrderBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onNetListener.onSuccess(orderBean);
                    }
                });
            }
        });
    }
}
net——Api

public interface Api {
    public static final String ORDER = "https://www.zhaoapi.cn/product/getOrders";
    public static final String CANCLE_ORDER = "https://www.zhaoapi.cn/product/updateOrder";
}

net——HttpUtils

import java.util.Map;

import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;


public class HttpUtils {
    private static HttpUtils httpUtils;
    private final OkHttpClient client;

    private HttpUtils() {
        //创建OkHttpClient
        //创建拦截器

        client = new OkHttpClient.Builder()
                .addInterceptor(new MyInterceptor())
                .build();
    }

    public static HttpUtils getHttpUtils() {
        if (httpUtils == null) {
            synchronized (HttpUtils.class) {
                if (httpUtils == null) {
                    httpUtils = new HttpUtils();
                }
            }
        }
        return httpUtils;
    }

    /**
     * GET请求
     *
     * @param url      请求地址
     * @param callback 回调
     */
    public void doGet(String url, Callback callback) {
        Request request = new Request.Builder().url(url).build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * POST请求
     *
     * @param url      请求地址
     * @param params   请求的参数
     * @param callback 回调
     */
    public void doPost(String url, Map<String, String> params, Callback callback) {
        //这里可以加网络判断

        //判断参数
        if (params == null || params.size() == 0) {
            throw new RuntimeException("params is null!!!");
        }
        // 创建Request
        FormBody.Builder builder = new FormBody.Builder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            builder.add(entry.getKey(), entry.getValue());
        }
        FormBody formBody = builder.build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        client.newCall(request).enqueue(callback);
    }
}
net——MyInterceptor

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;


public class MyInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        //获取原始的请求数据
        Request orginRequest = chain.request();
        //先获取原始的请求参数,然后再拼接起来
        FormBody orginBody = (FormBody) orginRequest.body();
        FormBody.Builder builder = new FormBody.Builder();
        for (int i = 0; i < orginBody.size(); i++) {
            String name = orginBody.name(i);
            String value = orginBody.value(i);
            builder.add(name, value);
        }
        builder.add("source", "android");
        FormBody formBody = builder.build();
        Request request = new Request.Builder().url(orginRequest.url()).post(formBody).build();

        return chain.proceed(request);

    }
}

net—— OnNetListener

public interface OnNetListener<T> {
    //成功回调
    public void onSuccess(T t);

    //失败回调
    public void onFailure(Exception e);
}

presenter—— CancleOrderPresenter

import com.example.week3.model.CancleOrderModel;
import com.example.week3.net.OnNetListener;

public class CancleOrderPresenter {
    private CancleOrderModel cancleOrderModel;

    public CancleOrderPresenter() {
        this.cancleOrderModel = new CancleOrderModel();
    }


    public void cancleOrder(String status, String orderId) {
        cancleOrderModel.cancleOrder(status, orderId, new OnNetListener() {
            @Override
            public void onSuccess(Object o) {

            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }
}
presenter—— MainPresenter

import com.example.week3.bean.OrderBean;
import com.example.week3.model.OrderModel;
import com.example.week3.net.OnNetListener;
import com.example.week3.view.IMainActivity;

public class MainPresenter {
    private OrderModel orderModel;
    private IMainActivity iMainActivity;

    public MainPresenter(IMainActivity iMainActivity) {
        orderModel = new OrderModel();
        this.iMainActivity = iMainActivity;
    }

    public void getOrder(final boolean isRefresh, String status, String page) {
        orderModel.getOrder(status, page, new OnNetListener<OrderBean>() {
            @Override
            public void onSuccess(OrderBean orderBean) {
                iMainActivity.showData(isRefresh, orderBean.getData());
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }

    public void dettach() {
        if (iMainActivity != null) {
            iMainActivity = null;
        }
    }
}

view—— IMainActivity

import com.example.week3.bean.OrderBean;
import java.util.List;

public interface IMainActivity {
    public void showData(boolean isRefresh, List<OrderBean.DataBean> list);
}
Main函数
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.week3.adapter.MyAdapter;
import com.example.week3.bean.OrderBean;
import com.example.week3.presenter.MainPresenter;
import com.example.week3.view.IMainActivity;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements IMainActivity, View.OnClickListener {
    /**
     * 待支付
     */
    private RadioButton mRb1;
    /**
     * 已支付
     */
    private RadioButton mRb2;
    /**
     * 已取消
     */
    private RadioButton mRb3;
    private RadioGroup mRg;
    private XRecyclerView mXrv;
    private MainPresenter mainPresenter;
    private int page = 1;
    private List<OrderBean.DataBean> list = new ArrayList<>();
    private MyAdapter myAdapter;
    private ImageView mIvPop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        mXrv.setLayoutManager(new LinearLayoutManager(this));
        myAdapter = new MyAdapter(this, list);
        mXrv.setAdapter(myAdapter);

        //想去获取数据,测试一下数据是否获取到
        mainPresenter = new MainPresenter(this);
        mainPresenter.getOrder(true, "1", page + "");
        mXrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //刷新
                page = 1;
                mainPresenter.getOrder(true, "1", page + "");

            }
            @Override
            public void onLoadMore() {
                //加载更多
                page++;
                mainPresenter.getOrder(false, "1", page + "");
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mainPresenter.dettach();
    }

    private void initView() {
        mRb1 = (RadioButton) findViewById(R.id.rb1);
        mRb2 = (RadioButton) findViewById(R.id.rb2);
        mRb3 = (RadioButton) findViewById(R.id.rb3);
        mRg = (RadioGroup) findViewById(R.id.rg);
        mXrv = (XRecyclerView) findViewById(R.id.xrv);
        mIvPop = (ImageView) findViewById(R.id.iv_pop);
        mIvPop.setOnClickListener(this);
    }

    @Override
    public void showData(boolean isRefresh, List<OrderBean.DataBean> newList) {
        if (isRefresh) {
            //如果是刷新
            list.clear();
            list.addAll(newList);
            mXrv.refreshComplete();
        } else {
            //如果是加载更多
            list.addAll(newList);
            mXrv.setLoadingMoreEnabled(false);
        }
        myAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.iv_pop:
                //弹出popupwindow
                View view = View.inflate(MainActivity.this, R.layout.pop_item, null);
                TextView tv1 = view.findViewById(R.id.tv1);
                TextView tv2 = view.findViewById(R.id.tv2);
                TextView tv3 = view.findViewById(R.id.tv3);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                PopupWindow popupWindow = new PopupWindow(view, layoutParams.width, layoutParams.height);
                popupWindow.showAsDropDown(mIvPop, 0, 30);

                tv1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, "tv1", Toast.LENGTH_SHORT).show();
                        mainPresenter.getOrder(true, "1", "1");
                    }
                });
                tv2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, "tv2", Toast.LENGTH_SHORT).show();
                    }
                });
                tv3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, "tv3", Toast.LENGTH_SHORT).show();
                    }
                });
                break;
        }
    }
}


//activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
//activity_main.xml
<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"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="订单列表"
            android:textSize="25sp" />

        <ImageView
            android:id="@+id/iv_pop"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            android:background="@drawable/lv_icon" />
    </RelativeLayout>

    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="待支付" />

        <RadioButton
            android:id="@+id/rb2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="已支付" />

        <RadioButton
            android:id="@+id/rb3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="已取消" />
    </RadioGroup>

    <com.jcodecraeer.xrecyclerview.XRecyclerView
        android:id="@+id/xrv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

//item.xml
<?xml version="1.0" encoding="utf-8"?>
//item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:descendantFocusability="blocksDescendants"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true" />

        <TextView
            android:id="@+id/tv_status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" />
    </RelativeLayout>

    <TextView
        android:id="@+id/tv_price"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_centerVertical="true"
        android:layout_weight="1"
        android:textColor="#ff0000" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TextView
            android:id="@+id/tv_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true" />
    </RelativeLayout>

</LinearLayout>

//pop_item.xml
<?xml version="1.0" encoding="utf-8"?>
//pop_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="待支付"></TextView>

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="已支付"></TextView>

    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="已取消"></TextView>
</LinearLayout>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值