如果想把购物车和订单联系起来,见地址:
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; } }
本文档详述了如何创建一个仿京东订单的Demo,包括使用各种工具类、设置订单Activity的布局,以及主页面的fragment设计。文章提供了将购物车与订单关联的方法链接,并提到了在开始编码前需要添加的权限和依赖。
1405

被折叠的 条评论
为什么被折叠?



