UserResultVoteFragment.java
6.87 KB
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
package com.sunvote.xpadapp.fragments;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.sunvote.xpadapp.MainActivity;
import com.sunvote.xpadapp.R;
import com.sunvote.xpadapp.base.BaseFragment;
import com.sunvote.xpadapp.db.modal.BillInfo;
import com.sunvote.xpadapp.db.modal.MultiTitleItem;
import java.text.DecimalFormat;
import java.util.ArrayList;
public class UserResultVoteFragment extends BaseFragment {
private byte[] data;
private String[] options;
public void setData(byte[] data) {
this.data = data;
}
public void setOptions(String[] options) {
this.options = options;
}
private TextView yingdaoresult;
private TextView shidaoresult;
// 表決结果
private TextView fragmentResultMemo;
private TextView fragmentMemo;
//
// private LinearLayout tResult;
// private LinearLayout dResult;
// private LinearLayout pResult;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_result_vote, container, false);
yingdaoresult = root.findViewById(R.id.yingdaoresult);
shidaoresult = root.findViewById(R.id.shidaoresult);
fragmentResultMemo = root.findViewById(R.id.fragment_result_memo);
fragmentMemo = root.findViewById(R.id.fragment_memo);
// tResult = root.findViewById(R.id.t_result);
// dResult = root.findViewById(R.id.d_result);
// pResult = root.findViewById(R.id.p_result);
showResult();
return root;
}
private View createView(String txt,int color) {
TextView tv1 = new TextView(getActivity());
ViewGroup.LayoutParams vlp = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
tv1.setLayoutParams(vlp);//设置TextView的布局
tv1.setText(txt);
tv1.setGravity(Gravity.CENTER);
tv1.setTextSize(25);
tv1.setTextColor(color);
return tv1;
}
int fenmu,goToBe,goTo,notVote;
private void showResult() {
int bitNum = data[2] & 0xf;
int digiters = data[2] >> 4 & 0xf;
int voteNo = data[3] & 0xff;
int resultSelectModel = data[4] & 0xff; //表决结果
if (bitNum == 0) { //单字节数值
fenmu = data[5] & 0xff;
goToBe= data[6] & 0xff;
goTo= data[7] & 0xff;
notVote= data[8] & 0xff;
int pos =9;
int i = pos;
for (; i < data.length; i++) {
if (pos == data.length - 1) {
break;
}
setView(pos,digiters,bitNum,1);
}
if (goToBe != 0xff) {
yingdaoresult.setText("" + goToBe);
}
if (goTo != 0xff) {
shidaoresult.setText("" + goTo);
}
if (resultSelectModel != 0xFF) {
if(options!=null){
fragmentResultMemo.setText(options[resultSelectModel]);
}
}
} else {
fenmu = (data[5] <<8 | data[6]) & 0xffff;
goToBe = (data[7] << 8 | data[8]) & 0xffff;
goTo = (data[9] << 8 | data[10]) & 0xffff;
notVote= (data[11] << 8 | data[12]) & 0xffff;
int pos = 13;
int i = pos;
for (; i < data.length; i++) {
if (pos == data.length - 1) {
break;
}
setView(pos,digiters,bitNum,2);
}
if (goToBe != 0xff) {
yingdaoresult.setText("" + goToBe);
}
if (goTo != 0xff) {
shidaoresult.setText("" + goTo);
}
if (resultSelectModel != 0xFF) {
if(options!=null){
fragmentResultMemo.setText(options[resultSelectModel]);
}
}
}
}
private void setView(int pos, int digiters, int bitNum, int type) {
// tResult.removeAllViews();
// dResult.removeAllViews();
// pResult.removeAllViews();
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT,1.0f);
for(int i = 1; i <= options.length ; i++) {
if (options.length >= i) {
int value1;
if (bitNum == 0) {
value1 = (data[pos++]) & 0xff;
} else {
value1 = (data[pos++] << 8 | data[pos++]) & 0xffff;
}
// tResult.addView(createView(options[i - 1], Color.YELLOW), lp);
// dResult.addView(createView("" + value1, Color.WHITE), lp);
if (type == 1) {
//分母
if ((fenmu > 0 && fenmu != 0xff)) {
double pzc = value1 * 1.0 / fenmu * 100;
String szc = formatDoubleToString(pzc, digiters, false);
// pResult.addView(createView(szc + "%", Color.WHITE), lp);
}
} else {
//分母
if ((fenmu > 0 && fenmu != 0xffff)) {
double pzc = value1 * 1.0 / fenmu * 100;
String szc = formatDoubleToString(pzc, digiters, false);
// pResult.addView(createView(szc + "%", Color.WHITE), lp);
}
}
}
}
if (notVote != 0xff) {
// tResult.addView(createView(getString(R.string.weian), Color.YELLOW), lp);
// dResult.addView(createView("" + notVote, Color.WHITE), lp);
if ((fenmu > 0 && fenmu != 0xff)) {
// pResult.addView(createView("0%", Color.WHITE), lp);
}else{
// pResult.setVisibility(View.GONE);
}
}
}
public static String formatDoubleToString(double value, Integer digits, boolean remove) {
if (value == 0 || value == 100) {
DecimalFormat df = new DecimalFormat("0");
return df.format(value);
}
if (digits == null || digits < 0) {
return String.valueOf(value);
} else if (digits == 0) {
DecimalFormat df = new DecimalFormat("0");
return df.format(value);
} else {
String temp = "0";
if (remove) {
temp = "#";
}
StringBuffer buffer = new StringBuffer("0.");
for (int i = 0; i < digits; i++) {
buffer.append(temp);
}
DecimalFormat df = new DecimalFormat(buffer.toString());
return df.format(value);
}
}
}