Android实战-智慧城市(7)

续前节,城市地铁功能开发Android实战-智慧城市(6)

软件设计

智慧巴士

    RecyclerView嵌套RecyclerView思路比较复杂,后边有一个Spinner比较陌生,剩下的都还好

巴士所有线路信息的反序列化对象

    所有巴士路线的所有信息的json的反序列化对象
    BusLineBean.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.SmartCity.Bus;

import java.util.List;

//巴士路线列表的反序列化对象
public class BusLineBean {
private int total;
private List<RowsBean> rows;
private int code;
private String msg;

public int getTotal() {
return total;
}

public void setTotal(int total) {
this.total = total;
}

public List<RowsBean> getRows() {
return rows;
}

public void setRows(List<RowsBean> rows) {
this.rows = rows;
}

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public static class RowsBean {
private Object searchValue;
private Object createBy;
private String createTime;
private Object updateBy;
private String updateTime;
private Object remark;
private ParamsBean params;
private int id;
private String name;
private String first;
private String end;
private String startTime;
private String endTime;
private double price;
private String mileage;

public Object getSearchValue() {
return searchValue;
}

public void setSearchValue(Object searchValue) {
this.searchValue = searchValue;
}

public Object getCreateBy() {
return createBy;
}

public void setCreateBy(Object createBy) {
this.createBy = createBy;
}

public String getCreateTime() {
return createTime;
}

public void setCreateTime(String createTime) {
this.createTime = createTime;
}

public Object getUpdateBy() {
return updateBy;
}

public void setUpdateBy(Object updateBy) {
this.updateBy = updateBy;
}

public String getUpdateTime() {
return updateTime;
}

public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
}

public Object getRemark() {
return remark;
}

public void setRemark(Object remark) {
this.remark = remark;
}

public ParamsBean getParams() {
return params;
}

public void setParams(ParamsBean params) {
this.params = params;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getFirst() {
return first;
}

public void setFirst(String first) {
this.first = first;
}

public String getEnd() {
return end;
}

public void setEnd(String end) {
this.end = end;
}

public String getStartTime() {
return startTime;
}

public void setStartTime(String startTime) {
this.startTime = startTime;
}

public String getEndTime() {
return endTime;
}

public void setEndTime(String endTime) {
this.endTime = endTime;
}

public double getPrice() {
return price;
}

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

public String getMileage() {
return mileage;
}

public void setMileage(String mileage) {
this.mileage = mileage;
}

public static class ParamsBean {
}
}
}

巴士路线的列表item布局

    因为是列表嵌套,所以在这个item中还有一个RecyclerView,默认它是不可见状态
    activity_bus_item.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical"
android:background="@color/white"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:text="lineName"
android:id="@+id/lineName"
android:gravity="center"
android:padding="10dp"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="@color/white"
android:background="@color/blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="始发站"
android:id="@+id/firstStation"
android:gravity="center"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<ImageView
android:src="@mipmap/mark_left"
android:scaleType="fitXY"
android:layout_width="120dp"
android:layout_height="10dp"/>
<ImageView
android:src="@mipmap/mark_right"
android:scaleType="fitXY"
android:layout_width="48dp"
android:layout_height="18dp"/>
<TextView
android:text="终点站"
android:id="@+id/endStation"
android:gravity="center"
android:textStyle="bold"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="票价: "
android:id="@+id/price"
android:gravity="center"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<TextView
android:text="里程: "
android:id="@+id/mileage"
android:gravity="center"
android:textStyle="bold"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</LinearLayout>
<View
android:background="@color/darker_gray"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<LinearLayout
android:orientation="horizontal"
android:gravity="center_vertical"
android:id="@+id/recyclerControl"
android:layout_width="match_parent"
android:layout_height="38dp">
<TextView
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:textColor="@color/black"
android:textSize="14sp"
android:textStyle="bold"
android:text="线路列表"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>
<ImageView
android:src="@mipmap/forword"
android:layout_marginRight="10dp"
android:layout_width="12dp"
android:layout_height="12dp"/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:visibility="gone"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

巴士线路列表的适配器

    适配器中有两个item的click,一个供外部调用显示和隐藏站点列表,一个点击跳转到定制班车
    BusAdapter.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.SmartCity.Bus;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.SmartCity.R;

import java.util.List;

public class BusAdapter extends RecyclerView.Adapter<BusAdapter.MyViewHolder> {
private List<String> lineName,firstStation,endStation,price,mileage;//路线名、起点、终点、票价、里程
private List<Integer> id;//路线id
private Context context;

public BusAdapter(List<String> lineName, List<String> firstStation, List<String> endStation, List<String> price, List<String> mileage, List<Integer> id, Context context) {
this.lineName = lineName;
this.firstStation = firstStation;
this.endStation = endStation;
this.price = price;
this.mileage = mileage;
this.context = context;
this.id = id;
}

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

@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.lineName.setText(lineName.get(position));
holder.firstStation.setText("始发站\n" + firstStation.get(position));
holder.endStation.setText("终点站\n" + endStation.get(position));
holder.price.setText("票价: " + price.get(position));
holder.mileage.setText("里程: " + mileage.get(position) + "km");
}

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

public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView lineName,firstStation,endStation,price,mileage;
private LinearLayout recyclerControl;
private RecyclerView recyclerView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
lineName = itemView.findViewById(R.id.lineName);
firstStation = itemView.findViewById(R.id.firstStation);
endStation = itemView.findViewById(R.id.endStation);
price = itemView.findViewById(R.id.price);
mileage = itemView.findViewById(R.id.mileage);
recyclerControl = itemView.findViewById(R.id.recyclerControl);
recyclerView = itemView.findViewById(R.id.recyclerView);
// 单击时二级列表显示或隐藏
recyclerControl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onRecyclerItemListener != null){
// 把线路id和列表对象作为参数传递
onRecyclerItemListener.onItemClick(id.get(getAbsoluteAdapterPosition()),recyclerView);
}
}
});
// 点击顶部的线路名就跳转到定制班车,并把线路id作为参数传递过去
lineName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context,BusMoreInfoActivity.class);
intent.putExtra("BusId",id.get(getAbsoluteAdapterPosition()));
context.startActivity(intent);
}
});
}
}

// 供外部调用,设置二级列表
private OnRecyclerItemListener onRecyclerItemListener;

public void setOnRecyclerItemListener(OnRecyclerItemListener onRecyclerItemListener) {
this.onRecyclerItemListener = onRecyclerItemListener;
}

public interface OnRecyclerItemListener{
void onItemClick(int id,RecyclerView recyclerView);
}
}

二级列表(线路的站点预览)的设置

    二级列表就是线路列表中的线路站点预览列表

二级列表站点的反序列化对象

    二级列表中线路所经过的所有站点的反序列化对象
    LineStationBean.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.SmartCity.Bus;

import java.util.List;

public class LineStationBean {

private int total;
private List<RowsBean> rows;
private int code;
private String msg;

public int getTotal() {
return total;
}

public void setTotal(int total) {
this.total = total;
}

public List<RowsBean> getRows() {
return rows;
}

public void setRows(List<RowsBean> rows) {
this.rows = rows;
}

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public static class RowsBean {
private Object searchValue;
private Object createBy;
private Object createTime;
private Object updateBy;
private Object updateTime;
private Object remark;
private ParamsBean params;
private int linesId;
private int stepsId;
private String name;
private int sequence;

public Object getSearchValue() {
return searchValue;
}

public void setSearchValue(Object searchValue) {
this.searchValue = searchValue;
}

public Object getCreateBy() {
return createBy;
}

public void setCreateBy(Object createBy) {
this.createBy = createBy;
}

public Object getCreateTime() {
return createTime;
}

public void setCreateTime(Object createTime) {
this.createTime = createTime;
}

public Object getUpdateBy() {
return updateBy;
}

public void setUpdateBy(Object updateBy) {
this.updateBy = updateBy;
}

public Object getUpdateTime() {
return updateTime;
}

public void setUpdateTime(Object updateTime) {
this.updateTime = updateTime;
}

public Object getRemark() {
return remark;
}

public void setRemark(Object remark) {
this.remark = remark;
}

public ParamsBean getParams() {
return params;
}

public void setParams(ParamsBean params) {
this.params = params;
}

public int getLinesId() {
return linesId;
}

public void setLinesId(int linesId) {
this.linesId = linesId;
}

public int getStepsId() {
return stepsId;
}

public void setStepsId(int stepsId) {
this.stepsId = stepsId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getSequence() {
return sequence;
}

public void setSequence(int sequence) {
this.sequence = sequence;
}

public static class ParamsBean {
}
}
}

二级列表的item布局

    简单到不能再简单的列表item
    activity_bus_line_item.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="38dp"
android:layout_width="match_parent"
android:layout_marginLeft="18dp"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<View
android:background="@color/blue"
android:layout_width="2dp"
android:layout_height="match_parent"/>
<TextView
android:text="stationName"
android:id="@+id/stationName"
android:gravity="center_vertical"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:textSize="14sp"
android:textColor="@color/black"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

二级列表的适配器

    简单到不能再简单的适配器
    BusLineAdapter.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.SmartCity.Bus;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.SmartCity.R;

import java.util.List;

public class BusLineAdapter extends RecyclerView.Adapter<BusLineAdapter.MyViewHolder> {
private List<String> stationName;//线路名
private Context context;

public BusLineAdapter(List<String> stationName, Context context) {
this.stationName = stationName;
this.context = context;
}

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

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.stationName.setText(stationName.get(position));
}

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

public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView stationName;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
stationName = itemView.findViewById(R.id.stationName);
}
}
}

定制班车

路线详情的发序列化对象

    路线的详细信息的json反序列化对象
    LineInfoBean.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.SmartCity.Bus;

public class LineInfoBean {

private String msg;
private int code;
private DataBean data;

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public DataBean getData() {
return data;
}

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

public static class DataBean {
private Object searchValue;
private Object createBy;
private String createTime;
private Object updateBy;
private String updateTime;
private Object remark;
private ParamsBean params;
private int id;
private String name;
private String first;
private String end;
private String startTime;
private String endTime;
private double price;
private String mileage;

public Object getSearchValue() {
return searchValue;
}

public void setSearchValue(Object searchValue) {
this.searchValue = searchValue;
}

public Object getCreateBy() {
return createBy;
}

public void setCreateBy(Object createBy) {
this.createBy = createBy;
}

public String getCreateTime() {
return createTime;
}

public void setCreateTime(String createTime) {
this.createTime = createTime;
}

public Object getUpdateBy() {
return updateBy;
}

public void setUpdateBy(Object updateBy) {
this.updateBy = updateBy;
}

public String getUpdateTime() {
return updateTime;
}

public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
}

public Object getRemark() {
return remark;
}

public void setRemark(Object remark) {
this.remark = remark;
}

public ParamsBean getParams() {
return params;
}

public void setParams(ParamsBean params) {
this.params = params;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getFirst() {
return first;
}

public void setFirst(String first) {
this.first = first;
}

public String getEnd() {
return end;
}

public void setEnd(String end) {
this.end = end;
}

public String getStartTime() {
return startTime;
}

public void setStartTime(String startTime) {
this.startTime = startTime;
}

public String getEndTime() {
return endTime;
}

public void setEndTime(String endTime) {
this.endTime = endTime;
}

public double getPrice() {
return price;
}

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

public String getMileage() {
return mileage;
}

public void setMileage(String mileage) {
this.mileage = mileage;
}

public static class ParamsBean {
}
}
}

路线所有站点的反序列化对象

    某一路线途径的所有站点的反序列化对象
    LineStationBean.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.SmartCity.Bus;

import java.util.List;

public class LineStationBean {

private int total;
private List<RowsBean> rows;
private int code;
private String msg;

public int getTotal() {
return total;
}

public void setTotal(int total) {
this.total = total;
}

public List<RowsBean> getRows() {
return rows;
}

public void setRows(List<RowsBean> rows) {
this.rows = rows;
}

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public static class RowsBean {
private Object searchValue;
private Object createBy;
private Object createTime;
private Object updateBy;
private Object updateTime;
private Object remark;
private ParamsBean params;
private int linesId;
private int stepsId;
private String name;
private int sequence;

public Object getSearchValue() {
return searchValue;
}

public void setSearchValue(Object searchValue) {
this.searchValue = searchValue;
}

public Object getCreateBy() {
return createBy;
}

public void setCreateBy(Object createBy) {
this.createBy = createBy;
}

public Object getCreateTime() {
return createTime;
}

public void setCreateTime(Object createTime) {
this.createTime = createTime;
}

public Object getUpdateBy() {
return updateBy;
}

public void setUpdateBy(Object updateBy) {
this.updateBy = updateBy;
}

public Object getUpdateTime() {
return updateTime;
}

public void setUpdateTime(Object updateTime) {
this.updateTime = updateTime;
}

public Object getRemark() {
return remark;
}

public void setRemark(Object remark) {
this.remark = remark;
}

public ParamsBean getParams() {
return params;
}

public void setParams(ParamsBean params) {
this.params = params;
}

public int getLinesId() {
return linesId;
}

public void setLinesId(int linesId) {
this.linesId = linesId;
}

public int getStepsId() {
return stepsId;
}

public void setStepsId(int stepsId) {
this.stepsId = stepsId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getSequence() {
return sequence;
}

public void setSequence(int sequence) {
this.sequence = sequence;
}

public static class ParamsBean {
}
}
}

站点列表的item

    注意这item是纵向的
    activity_bus_more_info_item.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="48dp"
android:orientation="vertical"
android:gravity="center_horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:src="@mipmap/subway"
android:layout_marginTop="10dp"
android:layout_width="18dp"
android:layout_height="18dp"/>
<View
android:layout_marginTop="10dp"
android:background="@color/blue"
android:layout_width="match_parent"
android:layout_height="2dp"/>
<TextView
android:text="n\na\nm\ne"
android:textStyle="bold"
android:textSize="16sp"
android:id="@+id/stationName"
android:textColor="@color/black"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

站点列表的反序列化对象

    因为列表是纵向的,所以这里给线路名中间加入了\n
    BusMoreInfoAdapter.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.SmartCity.Bus;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.SmartCity.R;

import java.util.List;

public class BusMoreInfoAdapter extends RecyclerView.Adapter<BusMoreInfoAdapter.MyViewHolder> {
private Context context;
private List<String> lineStationList;

public BusMoreInfoAdapter(Context context, List<String> lineStationList) {
this.context = context;
this.lineStationList = lineStationList;
}

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

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
String str = lineStationList.get(position).replaceAll("","\n");
holder.stationName.setText(str);
}

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

public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView stationName;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
stationName = itemView.findViewById(R.id.stationName);
}
}
}

界面布局

    spinner是比较陌生的,重点看一下
    activity_bus_more_info.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Title-->
<LinearLayout
android:background="@color/blue"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="58dp">
<LinearLayout
android:onClick="finishActivity"
android:gravity="center"
android:layout_width="58dp"
android:layout_height="58dp">
<ImageView
android:src="@mipmap/back"
android:layout_width="20dp"
android:layout_height="20dp"/>
</LinearLayout>
<LinearLayout
android:gravity="center"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
<TextView
android:text="定制班车"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="@color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:gravity="center"
android:layout_width="58dp"
android:layout_height="58dp">

</LinearLayout>
</LinearLayout>
<!-- Content-->
<ScrollView
android:background="@color/darker_gray"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_marginTop="10dp"
android:background="@color/white"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:gravity="center"
android:id="@+id/firstStation"
android:text="起点: "
android:textColor="@color/black"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"/>
<TextView
android:gravity="center"
android:text="终点: "
android:textColor="@color/black"
android:textSize="16sp"
android:id="@+id/endStation"
android:textStyle="bold"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"/>
</LinearLayout>
<View
android:background="@color/darker_gray"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<LinearLayout
android:background="@color/white"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:gravity="center"
android:text="票价: "
android:textColor="@color/black"
android:textSize="16sp"
android:id="@+id/price"
android:textStyle="bold"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"/>
<TextView
android:gravity="center"
android:text="里程: "
android:textColor="@color/black"
android:textSize="16sp"
android:textStyle="bold"
android:id="@+id/mileage"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_marginTop="10dp"
android:background="@color/white"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:paddingLeft="20dp"
android:text="日期选择"
android:textColor="@color/black"
android:textSize="14sp"
android:textStyle="bold"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:layout_height="match_parent"/>
<TextView
android:text="1999-01-01"
android:paddingRight="20dp"
android:textStyle="bold"
android:id="@+id/dateContent"
android:textSize="14sp"
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center_vertical|right"
android:layout_height="match_parent"/>
</LinearLayout>
<View
android:background="@color/darker_gray"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<LinearLayout
android:background="@color/white"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:paddingLeft="20dp"
android:text="时间选择"
android:textColor="@color/black"
android:textSize="14sp"
android:textStyle="bold"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:layout_height="match_parent"/>
<TextView
android:text="00:00"
android:paddingRight="20dp"
android:textStyle="bold"
android:textSize="14sp"
android:textColor="@color/black"
android:layout_width="0dp"
android:id="@+id/timeContent"
android:layout_weight="1"
android:gravity="center_vertical|right"
android:layout_height="match_parent"/>
</LinearLayout>
<View
android:background="@color/darker_gray"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<LinearLayout
android:background="@color/white"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:paddingLeft="20dp"
android:text="乘客姓名"
android:textColor="@color/black"
android:textSize="14sp"
android:textStyle="bold"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:layout_height="match_parent"/>
<EditText
android:background="@color/white"
android:hint="请输入乘客姓名"
android:paddingRight="20dp"
android:id="@+id/userName"
android:textStyle="bold"
android:textSize="14sp"
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_weight="1"
android:inputType="text"
android:lines="1"
android:gravity="center_vertical|right"
android:layout_height="match_parent"/>
</LinearLayout>
<View
android:background="@color/darker_gray"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<LinearLayout
android:background="@color/white"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:paddingLeft="20dp"
android:text="手机号码"
android:textColor="@color/black"
android:textSize="14sp"
android:textStyle="bold"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:layout_height="match_parent"/>
<EditText
android:background="@color/white"
android:hint="请输入手机号码"
android:paddingRight="20dp"
android:id="@+id/phoneNumber"
android:textStyle="bold"
android:textSize="14sp"
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_weight="1"
android:inputType="number"
android:lines="1"
android:gravity="center_vertical|right"
android:layout_height="match_parent"/>
</LinearLayout>
<View
android:background="@color/darker_gray"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<LinearLayout
android:background="@color/white"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:paddingLeft="20dp"
android:text="上车地点"
android:textColor="@color/black"
android:textSize="14sp"
android:textStyle="bold"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:layout_height="match_parent"/>
<!--设置android:textAlignment="viewEnd"使内容右对齐-->
<Spinner
android:textAlignment="viewEnd"
android:textStyle="bold"
android:textSize="14sp"
android:textColor="@color/black"
android:layout_width="0dp"
android:id="@+id/inStation"
android:layout_weight="1"
android:gravity="center_vertical|right"
android:layout_height="match_parent"
tools:ignore="RtlCompat" />
</LinearLayout>
<View
android:background="@color/darker_gray"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<LinearLayout
android:background="@color/white"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:paddingLeft="20dp"
android:text="下车地点"
android:textColor="@color/black"
android:textSize="14sp"
android:textStyle="bold"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:layout_height="match_parent"/>
<Spinner
android:textStyle="bold"
android:textSize="14sp"
android:textColor="@color/black"
android:textAlignment="viewEnd"
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/outStation"
android:gravity="center_vertical|right"
android:layout_height="match_parent"
tools:ignore="RtlCompat" />
</LinearLayout>
<Button
android:text="下一步"
android:textColor="@color/white"
android:textSize="16sp"
android:onClick="nextActivity"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/button_style2"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
</LinearLayout>

程序设计

    日期时间选择器、spinner动态加载数据
    BusMoreInfoActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package com.SmartCity.Bus;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.SmartCity.MyHttpRequest;
import com.SmartCity.R;

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

public class BusMoreInfoActivity extends AppCompatActivity implements View.OnClickListener {
private int BusId;//线路的id
private String BusName;//线路名
private LineInfoBean lineInfoBean;//路线详情的json反序列化对象
private LineStationBean lineStationBean;//查询线路所有站点信息的json反序列对象
private Handler handler;//线程通信
private MyHttpRequest myHttpRequest = new MyHttpRequest();//服务器通信模块封装
private List<String> lineStationList = new ArrayList<>();//线路中所有站点的列表
private TextView firstStation,endStation,price,mileage;//起点、终点、票价、里程
private RecyclerView recyclerView;//线路列表
private TextView dateContent,timeContent;//日期选择、时间选择
private EditText userName,phoneNumber;//乘客姓名、乘客联系电话
private Spinner inStation,outStation;//上车地点、下车地点
private String date,time;//日期、时间
private Calendar calendar = Calendar.getInstance();//获取时间日期

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bus_more_info);
myHandler();//线程通信
init();//初始化界面
submit();//设置监听
getData();//获取数据
}

// 线程通信
private void myHandler() {
handler = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what == 1){
setData();//验证数据正确性、预处理数据
}
}
};
}

// 验证数据正确性、预处理数据
private void setData() {
if (lineInfoBean == null || lineStationBean == null){
Toast.makeText(this,"线路信息获取错误: 未知原因",Toast.LENGTH_SHORT).show();
}
else if (lineStationBean.getCode() != 200){
Toast.makeText(this,"线路信息获取错误: " + lineStationBean.getMsg(),Toast.LENGTH_SHORT).show();
}
else if (lineInfoBean.getCode() != 200){
Toast.makeText(this,"线路信息获取错误: " + lineInfoBean.getMsg(),Toast.LENGTH_SHORT).show();
}
else {
for (int i = 0; i < lineStationBean.getTotal(); i++) {
lineStationList.add(lineStationBean.getRows().get(i).getName());
}
showBusInfo();//显示线路信息
}
}

// 显示线路信息
@SuppressLint("SetTextI18n")
private void showBusInfo() {
BusName = lineInfoBean.getData().getName();//线路名
firstStation.setText("起点: " + lineInfoBean.getData().getFirst());
endStation.setText("终点: " + lineInfoBean.getData().getEnd());
price.setText("票价: " + lineInfoBean.getData().getPrice());
mileage.setText("里程: " + lineInfoBean.getData().getMileage() + "km");

// 年月日,并给月和日去零
date = calendar.get(Calendar.YEAR) + "-" +
((calendar.get(Calendar.MONTH) + 1) < 10 ? "0" + (calendar.get(Calendar.MONTH) + 1) : calendar.get(Calendar.MONTH) + 1) + "-" +
(calendar.get(Calendar.DAY_OF_MONTH) < 10 ? "0" + calendar.get(Calendar.DAY_OF_MONTH) : calendar.get(Calendar.DAY_OF_MONTH));
dateContent.setText(date);
// 时分秒,并给分钟去“0”
time = calendar.get(Calendar.HOUR_OF_DAY) + ":" +
(calendar.get(Calendar.MINUTE) < 10 ? "0" + calendar.get(Calendar.MINUTE) : calendar.get(Calendar.MINUTE));
timeContent.setText(time);

// 设置第一个下拉列表中的列表项
ArrayAdapter<String> adapter1 =new ArrayAdapter<>(this, android.R.layout.simple_spinner_item,lineStationList);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
inStation.setAdapter(adapter1);//设置spinner的适配器
setOutStation();//下车点选择
setRecycler();//设置线路列表
}

// 当选中上车点的列表项时,动态获得下车点的列表项,防止下车点超过上车点
private void setOutStation() {
inStation.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
// 这里的position就是列表项中选中的item的position,根据position得到outStation中的列表数据
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
List<String> lineStationList2 = new ArrayList<>();
for (int i = position + 1; i < lineStationList.size(); i++) {
lineStationList2.add(lineStationList.get(i));
}
ArrayAdapter<String> adapter2 = new ArrayAdapter<>(BusMoreInfoActivity.this,android.R.layout.simple_spinner_item,lineStationList2);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
outStation.setAdapter(adapter2);
}

@Override
public void onNothingSelected(AdapterView<?> parent) { }
});
}


// 设置列表
private void setRecycler() {
BusMoreInfoAdapter busMoreInfoAdapter = new BusMoreInfoAdapter(this,lineStationList);
LinearLayoutManager layoutManager = new LinearLayoutManager(this,RecyclerView.HORIZONTAL,false);
recyclerView.setAdapter(busMoreInfoAdapter);
recyclerView.setLayoutManager(layoutManager);
}

// 获取线路数据
private void getData() {
lineInfoBean = (LineInfoBean) myHttpRequest.myHttp("/prod-api/api/bus/line/" + BusId,null,LineInfoBean.class,null,"get");
lineStationBean = (LineStationBean) myHttpRequest.myHttp("/prod-api/api/bus/stop/list?linesId=" + BusId,null,LineStationBean.class,null,"get");
Message message = Message.obtain();
message.what = 1;
handler.sendMessage(message);
}

// 初始化界面
private void init() {
BusId = getIntent().getExtras().getInt("BusId");//从跳转来的页面中获取到传入的线路id
firstStation = findViewById(R.id.firstStation);
endStation = findViewById(R.id.endStation);
price = findViewById(R.id.price);
mileage = findViewById(R.id.mileage);
recyclerView = findViewById(R.id.recyclerView);
dateContent = findViewById(R.id.dateContent);
timeContent = findViewById(R.id.timeContent);
userName = findViewById(R.id.userName);
phoneNumber = findViewById(R.id.phoneNumber);
inStation = findViewById(R.id.inStation);
outStation = findViewById(R.id.outStation);
}

// 设置监听
private void submit() {
dateContent.setOnClickListener(this);//日期选择
timeContent.setOnClickListener(this);//时间选择
}

// “下一步”按钮的OnClick
public void nextActivity(View view) {
String date = dateContent.getText().toString();//日期
String time = timeContent.getText().toString();//时间
String userName = this.userName.getText().toString();//乘客名
String phoneNumber = this.phoneNumber.getText().toString();//手机号
String inStation = this.inStation.getSelectedItem().toString();//上车地点
String outStation = this.outStation.getSelectedItem().toString();//下车地点
if (TextUtils.isEmpty(userName)){
Toast.makeText(this,"乘客姓名不可为空",Toast.LENGTH_SHORT).show();
}
else if (TextUtils.isEmpty(phoneNumber)){
Toast.makeText(this,"手机号不可为空",Toast.LENGTH_SHORT).show();
}
else if (phoneNumber.length() != 11){
Toast.makeText(this,"手机号格式不正确",Toast.LENGTH_SHORT).show();
}
else {
// 跳转到确认订单页
Intent intent = new Intent(this,ConfirmActivity.class);
intent.putExtra("date",date);
intent.putExtra("time",time);
intent.putExtra("userName",userName);
intent.putExtra("phoneNumber",phoneNumber);
intent.putExtra("inStation",inStation);
intent.putExtra("outStation",outStation);
intent.putExtra("BusName",BusName);
startActivity(intent);
}
}

// 结束页面
public void finishActivity(View view) { finish(); }

// 时间日期选择时
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.dateContent://日期选择器
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
String date = year + "-" +
((month + 1) < 10 ? "0" + (month + 1) : month + 1) + "-" +
(dayOfMonth < 10 ? "0" + dayOfMonth : dayOfMonth);
dateContent.setText(date);
}
},//日期选择监听器
calendar.get(Calendar.YEAR),//年
calendar.get(Calendar.MONTH),//月
calendar.get(Calendar.DAY_OF_MONTH));//日
datePickerDialog.show();//显示
break;
case R.id.timeContent://时间选择器
TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String time = hourOfDay + ":" +
(minute < 10 ? "0" + minute : minute);
timeContent.setText(time);
}
},//时间选择监听器
calendar.get(Calendar.HOUR_OF_DAY),//小时
calendar.get(Calendar.MINUTE),//分钟
true);//是否24小时制
timePickerDialog.show();
break;
}
}
}

确认订单

    显示订单信息,确认信息无误后点击“提交订单”就可以提交了

订单提交结果的反序列化对象

    提交订单后,返回的json的反序列化对象
    CommitOrderBean.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.SmartCity.Bus;

public class CommitOrderBean {

private String msg;
private int code;
private String orderNum;

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public String getOrderNum() {
return orderNum;
}

public void setOrderNum(String orderNum) {
this.orderNum = orderNum;
}
}

界面布局

    activity_confirm.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Title-->
<LinearLayout
android:background="@color/blue"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="58dp">
<LinearLayout
android:onClick="finishActivity"
android:gravity="center"
android:layout_width="58dp"
android:layout_height="58dp">
<ImageView
android:src="@mipmap/back"
android:layout_width="20dp"
android:layout_height="20dp"/>
</LinearLayout>
<LinearLayout
android:gravity="center"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
<TextView
android:text="确认订单"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="@color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:gravity="center"
android:layout_width="58dp"
android:layout_height="58dp">

</LinearLayout>
</LinearLayout>
<!-- Content-->
<ScrollView
android:background="@color/darker_gray"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:background="@color/white"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:text="线路名"
android:textSize="16sp"
android:textColor="@color/black"
android:textStyle="bold"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<TextView
android:text="lineName"
android:id="@+id/lineName"
android:layout_width="0dp"
android:textSize="16sp"
android:textColor="@color/black"
android:textStyle="bold"
android:gravity="center_vertical|right"
android:paddingRight="20dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
</LinearLayout>
<View
android:background="@color/darker_gray"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:text="乘客名"
android:textSize="16sp"
android:textColor="@color/black"
android:textStyle="bold"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<TextView
android:text="userName"
android:id="@+id/userName"
android:layout_width="0dp"
android:textSize="16sp"
android:textColor="@color/black"
android:textStyle="bold"
android:gravity="center_vertical|right"
android:paddingRight="20dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
</LinearLayout>
<View
android:background="@color/darker_gray"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:text="手机号"
android:textSize="16sp"
android:textColor="@color/black"
android:textStyle="bold"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<TextView
android:text="phoneNumber"
android:id="@+id/phoneNumber"
android:layout_width="0dp"
android:textSize="16sp"
android:textColor="@color/black"
android:textStyle="bold"
android:gravity="center_vertical|right"
android:paddingRight="20dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
</LinearLayout>
<View
android:background="@color/darker_gray"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:text="日期"
android:textSize="16sp"
android:textColor="@color/black"
android:textStyle="bold"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<TextView
android:text="date"
android:id="@+id/date"
android:layout_width="0dp"
android:textSize="16sp"
android:textColor="@color/black"
android:textStyle="bold"
android:gravity="center_vertical|right"
android:paddingRight="20dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
</LinearLayout>
<View
android:background="@color/darker_gray"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:text="时间"
android:textSize="16sp"
android:textColor="@color/black"
android:textStyle="bold"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<TextView
android:text="time"
android:id="@+id/time"
android:layout_width="0dp"
android:textSize="16sp"
android:textColor="@color/black"
android:textStyle="bold"
android:gravity="center_vertical|right"
android:paddingRight="20dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
</LinearLayout>
<View
android:background="@color/darker_gray"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:text="上车地点"
android:textSize="16sp"
android:textColor="@color/black"
android:textStyle="bold"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>

<TextView
android:id="@+id/inStation"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|right"
android:paddingRight="20dp"
android:text="inStation"
android:textColor="@color/black"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
<View
android:background="@color/darker_gray"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:text="下车地点"
android:textSize="16sp"
android:textColor="@color/black"
android:textStyle="bold"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<TextView
android:text="outStation"
android:id="@+id/outStation"
android:layout_width="0dp"
android:textSize="16sp"
android:textColor="@color/black"
android:textStyle="bold"
android:gravity="center_vertical|right"
android:paddingRight="20dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
</LinearLayout>
<View
android:background="@color/darker_gray"
android:layout_width="match_parent"
android:layout_height="1dp"/>

<LinearLayout
android:background="@color/darker_gray"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:onClick="commit"
android:textStyle="bold"
android:textColor="@color/white"
android:textSize="16sp"
android:text="提交订单"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:background="@drawable/button_style2"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>

程序设计

    订单提交成功后跳转到全部订单页面
    ConfirmActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.SmartCity.Bus;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.SmartCity.MyHttpRequest;
import com.SmartCity.R;
import com.SmartCity.SoftData;
import com.SmartCity.UserInfo.OrderActivity;

public class ConfirmActivity extends AppCompatActivity {
private Handler handler;//线程通信
private MyHttpRequest myHttpRequest = new MyHttpRequest();//服务器通信模块封装
private TextView lineName,userName,phoneNumber,date,time,inStation,outStation;
private String start,end,price,path;//上车点、下车点、票价、线路名
private int status = 0;//支付状态(0:未支付,1:已支付)
private CommitOrderBean commitOrderBean;//提交订单返回的json的反序列化对象

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirm);
myHandler();//线程通信
init();//初始化界面
getData();//获取提交订单的json字符串中的数据
setData();//显示上页填写的数据
}

private void myHandler() {
handler = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what == 1){
if (commitOrderBean == null){
Toast.makeText(ConfirmActivity.this,"订单提交失败: 未知原因",Toast.LENGTH_SHORT).show();
}
else if (commitOrderBean.getCode() != 200){
Toast.makeText(ConfirmActivity.this,"订单提交失败: " + commitOrderBean.getMsg(),Toast.LENGTH_SHORT).show();
}
else {
// 跳转到支付订单页面
Toast.makeText(ConfirmActivity.this,"订单提交成功",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(ConfirmActivity.this, OrderActivity.class);
startActivity(intent);
finish();//关闭当前页面
}
}
}
};
}

// 获取json中要提交的数据
private void getData() {
start = this.getIntent().getExtras().getString("inStation");
end = this.getIntent().getExtras().getString("outStation");
price = this.getIntent().getExtras().getString("price");
path = this.getIntent().getExtras().getString("BusName");
}

// 设置上页传入的数据
private void setData() {
lineName.setText(this.getIntent().getExtras().getString("BusName"));
userName.setText(this.getIntent().getExtras().getString("userName"));
phoneNumber.setText(this.getIntent().getExtras().getString("phoneNumber"));
date.setText(this.getIntent().getExtras().getString("date"));
time.setText(this.getIntent().getExtras().getString("time"));
inStation.setText(this.getIntent().getExtras().getString("inStation"));
outStation.setText(this.getIntent().getExtras().getString("outStation"));
}

// 初始化界面
private void init() {
lineName = findViewById(R.id.lineName);
userName = findViewById(R.id.userName);
phoneNumber = findViewById(R.id.phoneNumber);
date = findViewById(R.id.date);
time = findViewById(R.id.time);
inStation = findViewById(R.id.inStation);
outStation = findViewById(R.id.outStation);
}

// 提交按钮
public void commit(View view) {
String json = "{\n" +
"\"start\":\"" + start + "\",\n" +
"\"end\":\"" + end + "\",\n" +
"\"price\":\"" + price + "\",\n" +
"\"path\":\"" + path + "\",\n" +
"\"status\":" + status + "\n" +
"}";
commitOrderBean = (CommitOrderBean) myHttpRequest.myHttp("/prod-api/api/bus/order",json,CommitOrderBean.class, SoftData.token,"post");
Message message = Message.obtain();
message.what = 1;
handler.sendMessage(message);
}

// 关闭页面
public void finishActivity(View view) { finish(); }
}

全部订单

    全部订单页因为之前用不到,好像是直接跳过去了的

顶部按钮的两种状态

    蓝色边框白色背景
    outline_style.xml

1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="2dp"
android:color="@color/blue"/>
<solid android:color="@color/white"/>
</shape>

    蓝色背景
    outline_style2.xml
1
2
3
4
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/blue"/>
</shape>

    背景选择器
    order_button_style.xml

1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/outline_style2"/>
<item android:state_selected="false" android:drawable="@drawable/outline_style"/>
</selector>

订单的反序列化对象

    当前账号下的所有订单及订单信息的反序列化对象
    OrderBean.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.SmartCity.UserInfo;

import java.util.List;

public class OrderBean {

private int total;
private List<RowsBean> rows;
private int code;
private String msg;

public int getTotal() {
return total;
}

public void setTotal(int total) {
this.total = total;
}

public List<RowsBean> getRows() {
return rows;
}

public void setRows(List<RowsBean> rows) {
this.rows = rows;
}

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public static class RowsBean {
private int id;
private String orderNum;
private String path;
private String start;
private String end;
private int price;
private String userName;
private String userTel;
private int userId;
private int status;
private String paymentType;
private String payTime;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getOrderNum() {
return orderNum;
}

public void setOrderNum(String orderNum) {
this.orderNum = orderNum;
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public String getStart() {
return start;
}

public void setStart(String start) {
this.start = start;
}

public String getEnd() {
return end;
}

public void setEnd(String end) {
this.end = end;
}

public int getPrice() {
return price;
}

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

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getUserTel() {
return userTel;
}

public void setUserTel(String userTel) {
this.userTel = userTel;
}

public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}

public int getStatus() {
return status;
}

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

public String getPaymentType() {
return paymentType;
}

public void setPaymentType(String paymentType) {
this.paymentType = paymentType;
}

public String getPayTime() {
return payTime;
}

public void setPayTime(String payTime) {
this.payTime = payTime;
}
}
}

订单列表的item

    使用了一个蓝色背景框的样式
    activity_order_item.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:text="订单编号"
android:textSize="16sp"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textColor="@color/black"
android:background="@color/white"
android:gravity="center_vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<TextView
android:text="订单编号"
android:textSize="16sp"
android:textStyle="bold"
android:id="@+id/orderNumber"
android:textColor="@color/black"
android:background="@color/white"
android:gravity="center_vertical|right"
android:paddingRight="20dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
</LinearLayout>
<View
android:background="@color/darker_gray"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:text="线路"
android:textSize="16sp"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textColor="@color/black"
android:background="@color/white"
android:gravity="center_vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<TextView
android:text="线路名"
android:textSize="16sp"
android:textStyle="bold"
android:id="@+id/lineName"
android:textColor="@color/black"
android:background="@color/white"
android:gravity="center_vertical|right"
android:paddingRight="20dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
</LinearLayout>
<View
android:background="@color/darker_gray"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:text="票价"
android:textSize="16sp"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textColor="@color/black"
android:background="@color/white"
android:gravity="center_vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<TextView
android:text="¥8"
android:textSize="16sp"
android:textStyle="bold"
android:id="@+id/price"
android:textColor="@color/black"
android:background="@color/white"
android:gravity="center_vertical|right"
android:paddingRight="20dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
</LinearLayout>
<TextView
android:text="支付订单"
android:gravity="center"
android:background="@drawable/order_button_style"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="16sp"
android:id="@+id/orderPay"
android:layout_width="match_parent"
android:layout_height="48dp"/>
</LinearLayout>

订单列表的适配器

    支付按钮的click供外部调用,如果已经支付过的订单,则不显示支付按钮
    OrderAdapter.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.SmartCity.UserInfo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.SmartCity.R;

import java.util.List;

public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.MyViewHolder> {
private List<List<String>> pay;
private Context context;

public OrderAdapter(List<List<String>> pay, Context context) {
this.pay = pay;
this.context = context;
}

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

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.orderNumber.setText(pay.get(0).get(position));
holder.lineName.setText(pay.get(1).get(position));
holder.price.setText("¥" + pay.get(2).get(position));
// 如果是已经支付过的,则不显示支付按钮
if (pay.get(3).get(position).equals("1")){
holder.orderPay.setVisibility(View.GONE);
}
else {
holder.orderPay.setVisibility(View.VISIBLE);
}
}

@Override
public int getItemCount() {
return pay.get(0).size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView orderNumber,lineName,price,orderPay;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
orderNumber = itemView.findViewById(R.id.orderNumber);
lineName = itemView.findViewById(R.id.lineName);
price = itemView.findViewById(R.id.price);
orderPay = itemView.findViewById(R.id.orderPay);
// 支付按钮
orderPay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onRecyclerListener != null){
onRecyclerListener.OnItemClick(pay.get(0).get(getAbsoluteAdapterPosition()));
}
}
});
}
}

private OnRecyclerListener onRecyclerListener;

public void setOnRecyclerListener(OnRecyclerListener onRecyclerListener) {
this.onRecyclerListener = onRecyclerListener;
}

public interface OnRecyclerListener{
void OnItemClick(String orderNumber);
}
}

界面布局

    注意理解顶部两个按钮的状态改变
    activity_order.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Title-->
<LinearLayout
android:background="@color/blue"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="58dp">
<LinearLayout
android:onClick="finishActivity"
android:gravity="center"
android:layout_width="58dp"
android:layout_height="58dp">
<ImageView
android:src="@mipmap/back"
android:layout_width="20dp"
android:layout_height="20dp"/>
</LinearLayout>
<LinearLayout
android:gravity="center"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
<TextView
android:text="我的订单"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="@color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:gravity="center"
android:layout_width="58dp"
android:layout_height="58dp">

</LinearLayout>
</LinearLayout>
<!-- Content-->
<LinearLayout
android:background="@color/darker_gray"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_marginRight="60dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:background="@drawable/order_button_style"
android:text="未支付"
android:textSize="16sp"
android:id="@+id/button0"
android:textColor="@color/white"
android:textStyle="bold"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<TextView
android:text="已支付"
android:gravity="center"
android:id="@+id/button1"
android:textSize="16sp"
android:textColor="@color/black"
android:textStyle="bold"
android:background="@drawable/order_button_style"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>

程序设计

    通过一个过渡集合,在“支付”和“未支付”两个集合中来回克隆,使一个RecyclerView刷新数据,达到两个RecyclerView的效果
    OrderActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package com.SmartCity.UserInfo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.SmartCity.Bean;
import com.SmartCity.MyHttpRequest;
import com.SmartCity.R;
import com.SmartCity.SoftData;

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

public class OrderActivity extends AppCompatActivity implements View.OnClickListener {
private MyHttpRequest myHttpRequest = new MyHttpRequest();//服务器通信模块封装
private Handler handler;//线程通信模块
private OrderBean orderBean;//所有订单的json字符串反序列化对象
private List<List<String>> alreadyPay;//已支付订单
private List<List<String>> noPay;//未支付订单
private List<List<String>> pay;//克隆1或2,最终传入adapter
private RecyclerView recyclerView;//订单列表
private OrderAdapter orderAdapter;//适配器
private TextView button0,button1;//顶部的两个按钮
private Bean bean;//默认的反序列化列表

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
myHandler();//线程通信
init();//初始化界面
getData();//获取所有订单数据
}

// 获取所有订单数据
private void getData() {
new Thread(){
@Override
public void run() {
super.run();
orderBean = (OrderBean) myHttpRequest.myHttp("/prod-api/api/bus/order/list",null,OrderBean.class, SoftData.token,"get");
Message message = Message.obtain();
message.what = 1;
handler.sendMessage(message);
}
}.start();
}

// 线程通信
private void myHandler() {
handler = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what == 1){
setData(0);
}
}
};
}

// 将支付或未支付的订单的集合克隆到最终传入到adapter的集合中
private void setData(int status) {
setList();//创建集合、拿到数据
setButtonStyle(status);//设置按钮样式
// 判断显示未支付或者已支付的订单
if (status == 0){
pay.addAll(noPay);
}
else {
pay.addAll(alreadyPay);
}
setRecycler();//设置列表显示数据
}

// 设置按钮样式
private void setButtonStyle(int status) {
if (status == 0){
button0.setTextColor(getResources().getColor(R.color.white));
button0.setSelected(true);
button1.setTextColor(getResources().getColor(R.color.black));
button1.setSelected(false);
}
else if (status == 1){
button1.setTextColor(getResources().getColor(R.color.white));
button1.setSelected(true);
button0.setTextColor(getResources().getColor(R.color.black));
button0.setSelected(false);
}
}

// 设置列表显示数据
private void setRecycler() {
if (orderAdapter == null){
orderAdapter = new OrderAdapter(pay,this);
LinearLayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL,false);
recyclerView.setAdapter(orderAdapter);
recyclerView.setLayoutManager(layoutManager);
}
else {
orderAdapter.notifyDataSetChanged();
}
orderAdapter.setOnRecyclerListener(new OrderAdapter.OnRecyclerListener() {
@Override
public void OnItemClick(String orderNumber) {
payOrder(orderNumber);//支付订单,传入订单号
}
});
}

// 点击支付订单
private void payOrder(String orderNumber) {
String json = "{\n" +
"\"orderNum\": \"" + orderNumber + "\",\n" +
"\"paymentType\": \"电子支付\"\n" +
"}";
bean = (Bean) myHttpRequest.myHttp("/prod-api/api/bus/pay",json,Bean.class,SoftData.token,"post");
if (bean == null){
Toast.makeText(this,"支付失败: 未知原因",Toast.LENGTH_SHORT).show();
}
else if (bean.getCode() != 200){
Toast.makeText(this,"支付失败: " + bean.getMsg(),Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this,"支付成功",Toast.LENGTH_SHORT).show();
getData();//重新获得数据
}
}

// 得到的集合中的数据
private void setList() {
if (pay == null){
pay = new ArrayList<>();//只用作克隆
noPay = new ArrayList<>();
alreadyPay = new ArrayList<>();
// 创建集合中的集合
for (int i = 0; i < 4; i++) {
noPay.add(new ArrayList<>());
alreadyPay.add(new ArrayList<>());
}
}
else {
pay.clear();
// 清空集合中的集合
for (int i = 0; i < 4; i++) {
noPay.get(i).clear();
alreadyPay.get(i).clear();
}
}
// 得到集合数据
for (int i = 0; i < orderBean.getTotal(); i++) {
if (orderBean.getRows().get(i).getStatus() == 0){
noPay.get(0).add(orderBean.getRows().get(i).getOrderNum());
noPay.get(1).add(orderBean.getRows().get(i).getPath());
noPay.get(2).add(String.valueOf(orderBean.getRows().get(i).getPrice()));
noPay.get(3).add(String.valueOf(orderBean.getRows().get(i).getStatus()));
}
else if(orderBean.getRows().get(i).getStatus() == 1){
alreadyPay.get(0).add(orderBean.getRows().get(i).getOrderNum());
alreadyPay.get(1).add(orderBean.getRows().get(i).getPath());
alreadyPay.get(2).add(String.valueOf(orderBean.getRows().get(i).getPrice()));
alreadyPay.get(3).add(String.valueOf(orderBean.getRows().get(i).getStatus()));
}
}
}

// 初始化界面
private void init() {
recyclerView = findViewById(R.id.recyclerView);
button0 = findViewById(R.id.button0);
button1 = findViewById(R.id.button1);
button0.setOnClickListener(this);
button1.setOnClickListener(this);
button0.setSelected(true);
}

// 结束页面
public void finishActivity(View view) { finish(); }

// 顶部两个按钮点击时
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button0:
// 为0时加载未支付列表
setData(0);
break;
case R.id.button1:
// 为1时加载支付列表
setData(1);
break;
}
}
}



比赛不让用ViewPager2,其它都能忍,这个忍不了,不写了,操!
给开放个阿里云仓,谁想用什么自己找,谁本事大谁赢不就是了?