仿京東訂單Deom

如果想把購物車和訂單聯繫起來,見地址:

http://blog.csdn.net/biggrand/article/details/79081808

寫代碼前要先加上權限和依賴:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
依賴:

compile 'com.android.support:design:26.+'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.xhb:xbanner:1.2.9'
compile 'com.github.bumptech.glide:glide:3.6.0'
compile 'com.squareup.okhttp3:okhttp:3.6.0'
compile 'com.squareup.okio:okio:1.11.0'
compile 'com.android.support:recyclerview-v7:26.+'

各種工具類:

可能用到的接口:
//創建訂單
public static final String CHUANGJIAN_API = "https://www.zhaoapi.cn/product/createOrder";
//訂單列表
public static final String DINGDAN_API = "https://www.zhaoapi.cn/product/getOrders?uid=2845";
//修改訂單
public static final String UpDateDingDan_API = "https://www.zhaoapi.cn/product/updateOrder?uid=2845";
一個比較實用的工具類 :    CommonUtils
/**
 * 一些操作的工具類
 */
public class CommonUtils {
    
    private static SharedPreferences sharedPreferences;

    /**
     * DashApplication.getAppContext()可以使用,但是會使用系統默認的主題樣式,如果你自定義了某些樣式可能不會被使用
     * @param layoutId
     * @return
     */
    public static View inflate(int layoutId) {
        View view = View.inflate(MyApplication.getAppContext(), layoutId, null);
        return view;
    }

    /**
     * 自己寫的運行在主線程的方法
     * 如果是主線程,執行任務,否則使用handler發送到主線程中去執行
     *
     *
     * @param runable
     */
    public static void runOnUIThread(Runnable runable) {
        //先判斷當前屬於子線程還是主線程
        if (android.os.Process.myTid() == MyApplication.getMainThreadId()) {
            runable.run();
        } else {
            //子線程
            MyApplication.getAppHanler().post(runable);
        }
    }
}
MyApplicaion:
/**
 * Created by Administrator
 */

public class MyApplication extends Application {

    private static Context context;
    private static Handler handler;
    private static int mainId;
    public static boolean isLoginSuccess;//是否已經登錄的狀態

    @Override
    public void onCreate() {
        super.onCreate();
        //關於context----http://blog.csdn.net/lmj623565791/article/details/40481055
        context = getApplicationContext();
        //初始化handler
        handler = new Handler();
        //主線程的id
        mainId = Process.myTid();
    }

    /**
     * 一下三個訂單用
     */


    /**
     * 對外提供了context
     * @return
     */
    public static Context getAppContext() {
        return context;
    }

    /**
     * 得到全局的handler
     * @return
     */
    public static Handler getAppHanler() {
        return handler;
    }

    /**
     * 獲取主線程id
     * @return
     */
    public static int getMainThreadId() {
        return mainId;
    }
}
寫完之後別忘了再清單列表中配置:

訂單的Activity:

先佈局

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="DingDanActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="訂單列表"
            android:textSize="20dp" />

        <ImageView
            android:id="@+id/image"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_alignParentRight="true"
            android:layout_marginRight="20dp"
            android:src="@drawable/lv_icon" />
    </RelativeLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_daifukuan"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="待付款"/>
        <Button
            android:id="@+id/btn_yiwancheng"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="已完成"/>
        <Button
            android:id="@+id/btn_yiquxiao"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="已取消"/>
    </LinearLayout>

   <FrameLayout
       android:id="@+id/frameLayout"
       android:layout_width="match_parent"
       android:layout_height="match_parent"></FrameLayout>
</LinearLayout>

主頁面

public class DingDanActivity extends AppCompatActivity implements View.OnClickListener {
    private PopupWindow popupMenu;
    private ImageView imageView;
    private List<String> list;
    private Button daifukuan;
    private Button yiwancheng;
    private Button yiquxiao;
    private FrameLayout frameLayout;
    private DingDanFragment fragment;
    private String[] strings;

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


        list = new ArrayList<>();

        list.add("待付款");
        list.add("已完成");
        list.add("已取消");
        strings = new String[]{"待付款","已完成","已取消"};
        //圖片的點擊事件
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopWindow();
            }
        });

        fragment = new DingDanFragment();

        getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout,fragment).commit();

    }

    private void findView() {
        imageView = findViewById(R.id.image);
        daifukuan = findViewById(R.id.btn_daifukuan);
        yiwancheng = findViewById(R.id.btn_yiwancheng);
        yiquxiao = findViewById(R.id.btn_yiquxiao);
        frameLayout = findViewById(R.id.frameLayout);
    }

    //popwindow顯示
    private void showPopWindow() {
        //設置contentView
        View contentView = LayoutInflater.from(DingDanActivity.this).inflate(R.layout.popwindow_item, null);
        popupMenu = new PopupWindow(contentView);
        popupMenu.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupMenu.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //設置各個控件的點擊響應
        TextView tv1 = contentView.findViewById(R.id.pop_dai);
        TextView tv2 = contentView.findViewById(R.id.pop_wancheng);
        TextView tv3 = contentView.findViewById(R.id.pop_quxiao);
        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);

        popupMenu.showAsDropDown(imageView);

    }


    /**
     * 點擊popwindow裏的內容
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_daifukuan:
                getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout,fragment).commit();

                break;
            case R.id.btn_yiwancheng:
                getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout,fragment).commit();

                break;
            case R.id.btn_yiquxiao:
                getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout,fragment).commit();

                break;


            case R.id.pop_dai:
                popupMenu.dismiss();
                break;
            case R.id.pop_wancheng:
                popupMenu.dismiss();
                break;
            case R.id.pop_quxiao:
                popupMenu.dismiss();
                break;
            default:
                break;
        }
    }
}
因爲訂單裏把數據放在放入framlayout中替換,所有就把數據寫到fragment中,
fragment佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>
</LinearLayout>

放訂單數據的fragment
public class DingDanFragment extends Fragment {

    private View view;
    private List<DingDanBean.DataBean> dingdanList;
    private ListView listView;
    private DingDanBean dingDanBean;
    private DingDanAdapter adapter;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {

                if (dingdanList.size() > 0) {
                    setAdapter();
                    add();

                } else {
                    Toast.makeText(getActivity(), "沒有此類型的商品訂單哦", Toast.LENGTH_SHORT).show();
                }

            } else if (msg.what == 2) {
                add();
                setAdapter();
            }
        }
    };


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = View.inflate(getActivity(), R.layout.dingdan_layout, null);
        listView = view.findViewById(R.id.listView);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //拿到傳過來的值
        add();

    }

    private void add() {
        OkHttp3Util.doGet(Api.DINGDAN_API, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    final String string = response.body().string();
                    CommonUtils.runOnUIThread(new Runnable() {
                        @Override
                        public void run() {
                            dingDanBean = new Gson().fromJson(string, DingDanBean.class);
                            dingdanList = dingDanBean.getData();
                            dingdanList.addAll(dingDanBean.getData());
                            Log.i("------dal", dingdanList.toString());
                            handler.sendEmptyMessage(1);
                        }
                    });
                }
            }
        });
    }

    //設置適配器
    private void setAdapter() {
        if (adapter == null) {
            adapter = new DingDanAdapter(getActivity(), dingdanList, handler);
            listView.setAdapter(adapter);

        } else {
            adapter.notifyDataSetChanged();
        }
    }

}
適配器的佈局:
<?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"
    android:orientation="horizontal"
    >
    
    <LinearLayout
        android:paddingLeft="35dp"
      android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_weight="3"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:textColor="#000"
            android:id="@+id/dingtitile"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:id="@+id/dingprice"
            android:textColor="#f00"
            android:textSize="20dp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:id="@+id/dingtime"

            />

    </LinearLayout>

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/zhuangtai"
            android:layout_marginLeft="10dp"
            android:text="yi"
            />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="訂單"
            android:id="@+id/dan"
            />

    </LinearLayout>

</LinearLayout>
訂單適配器:
public class DingDanAdapter extends BaseAdapter {

    private List<DingDanBean.DataBean> list;
    private Handler handler;
    private Context context;

    public DingDanAdapter(Context context, List<DingDanBean.DataBean> list, Handler handler) {
        this.context = context;
        this.list = list;
        this.handler = handler;
    }

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

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = View.inflate(context, R.layout.dingdan_item, null);
            holder.titile = view.findViewById(R.id.dingtitile);
            holder.price = view.findViewById(R.id.dingprice);
            holder.time = view.findViewById(R.id.dingtime);
            holder.zt = view.findViewById(R.id.zhuangtai);
            holder.dan = view.findViewById(R.id.dan);

            view.setTag(holder);

        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.titile.setText(list.get(i).getTitle());
        holder.price.setText("價格:" + list.get(i).getPrice());
        holder.time.setText("時間:" + list.get(i).getCreatetime());

        Log.d("ffff", list.get(i).getStatus() + "");
        if (list.get(i).getStatus() == 0) {
            holder.zt.setText("待付款");
            holder.zt.setTextColor(Color.RED);
        } else if (list.get(i).getStatus() == 1) {
            holder.zt.setText("已支付");
        } else if (list.get(i).getStatus() == 2) {
            holder.zt.setText("已取消");
        }


        if (list.get(i).getStatus() == 0) {
            holder.dan.setText("取消訂單");
            holder.dan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final AlertDialog.Builder ab = new AlertDialog.Builder(context);
                    ab.setTitle("確認取消訂單嗎?");
                    ab.setPositiveButton("", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(final DialogInterface dialogInterface, final int j) {

                            OkHttp3Util.doGet(Api.UpDateDingDan_API + "&status=2&orderId=" + list.get(i).getOrderid(), new Callback() {
                                @Override
                                public void onFailure(Call call, IOException e) {

                                }

                                @Override
                                public void onResponse(Call call, Response response) throws IOException {
                                    if (response.isSuccessful()) {
                                        final String string = response.body().string();
                                        CommonUtils.runOnUIThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Toast.makeText(context, string, Toast.LENGTH_SHORT).show();

                                                OkHttp3Util.doGet(Api.DINGDAN_API, new Callback() {
                                                    @Override
                                                    public void onFailure(Call call, IOException e) {

                                                    }

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

                                                        final String string = response.body().string();
                                                        if (response.isSuccessful()) {

                                                            CommonUtils.runOnUIThread(new Runnable() {
                                                                @Override
                                                                public void run() {
                                                                    Gson gson = new Gson();
                                                                    DingDanBean dingbean = gson.fromJson(string, DingDanBean.class);
                                                                    list.clear();
                                                                    list.addAll(dingbean.getData());
                                                                    holder.zt.setTextColor(Color.GRAY);

                                                                    notifyDataSetChanged();
                                                                    List<DingDanBean.DataBean> data = dingbean.getData();

                                                                    Message message = Message.obtain();
                                                                    message.obj = data;

                                                                    message.what = 2;
                                                                    handler.sendMessage(message);

                                                                }
                                                            });
                                                        }


                                                    }
                                                });

                                            }
                                        });
                                    }


                                }
                            });


                        }
                    });
                    ab.setNegativeButton("", null);
                    ab.show();
                }
            });


        } else {
            holder.dan.setText("查看訂單");
        }

        return view;
    }

    public class ViewHolder {
        Button dan;
        TextView zt;
        TextView time;
        TextView price;
        TextView titile;
    }
}

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