ResultElectionCustomFragment.java 11.9 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 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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
package com.sunvote.xpadapp.fragments;

import android.annotation.SuppressLint;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.sunvote.util.LogUtil;
import com.sunvote.xpadapp.MainActivity;
import com.sunvote.xpadapp.R;
import com.sunvote.xpadapp.base.BaseFragment;
import com.sunvote.xpadapp.base.TextViewUtils;
import com.sunvote.xpadapp.db.modal.BillInfo;
import com.sunvote.xpadapp.db.modal.MultiTitleItem;
import com.sunvote.xpadcomm.XPadApiInterface;
import com.ycanfunc.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import static com.sunvote.xpadapp.utils.MyStringUtil.replaceBlank;

public class ResultElectionCustomFragment extends BaseFragment {

	public byte[] data;
	public BillInfo bill;
	public XPadApiInterface.VoteInfo voteInfo;
	String TAG = "ResultMultiVoteFragment";

	public ArrayList<BillInfo> subInfo;
	String[] options;

	private ArrayList<ListItem> aryContent = null;

	private TextView tvTitle;
	private TextView titleAbstain;
	private TextView titleUnvote;
	private TextView titleResult;

	private ListView listview;
	private MyAdapter mAdapter;
	private int resultBits;

	int fenmu;
	int yindao;
	int shidao;

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		LogUtil.d(TAG, "onCreateView");
		View view = inflater.inflate(R.layout.fragment_result_multi_vote, container, false);
		final MainActivity mact = (MainActivity) getActivity();

		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				;
			}
		});

		listview = (ListView) view.findViewById(R.id.multi_vote_result_listview);
		options = bill.billOptions.split("/");

		try {
			initData();
		}catch (Exception ex){ex.printStackTrace();}

		mAdapter = new MyAdapter(getActivity());
		listview.setAdapter(mAdapter);

		tvTitle = (TextView) view.findViewById(R.id.multi_vote_result_tv_title);
		if (bill != null) {
			tvTitle.setText(  replaceBlank(bill.title));
		}

		titleAbstain = (TextView) view.findViewById(R.id.multi_vote_result_title_abstan);
		titleUnvote = (TextView) view.findViewById(R.id.multi_vote_result_title_unvote);
		titleResult = (TextView) view.findViewById(R.id.multi_vote_result_title_result);

		if (resultBits == 0xffff) {
			titleResult.setVisibility(View.GONE);
		} else {
			titleResult.setVisibility(View.VISIBLE);
		}
		if (options.length == 2) {
			titleAbstain.setVisibility(View.GONE);
		} else {
			titleAbstain.setVisibility(View.VISIBLE);
		}
		TextView tvYindao = (TextView) view.findViewById(R.id.multi_vote_result_yindao);
		TextView tvShidao = (TextView) view.findViewById(R.id.multi_vote_result_shidao);
		if (yindao == 0xff || yindao == 0xffff) {
			tvYindao.setVisibility(View.GONE);
		} else {
			tvYindao.setText("" + + yindao);
			tvYindao.setVisibility(View.VISIBLE);
		}

		if (shidao == 0xff || shidao == 0xffff) {
			tvShidao.setVisibility(View.GONE);
		} else {
			tvShidao.setText("" + shidao);
			tvShidao.setVisibility(View.VISIBLE);
		}

		return view;
	}

	private void initData() {

		int bitNum = data[2] & 0xf;
		int digiters = data[2] >> 4 & 0xf;
		int voteNo = data[3] & 0xff;
		int startNo = ((data[4] & 0xff) << 8) | (data[5] & 0xff) ;
		int endNo =   ((data[6] & 0xff) << 8) | (data[7] & 0xff) ;
		int rH = data[8] & 0xff;
		int rL = data[9] & 0xff;
		resultBits = rH << 8 | rL;

		// resultBits = ((data[8] << 8) | data[9]) & 0xff;

		// Log.d(TAG, "data len:" + data.length);
		// Toast.makeText(mMainActivity, "data len:" + data.length,
		// Toast.LENGTH_LONG).show();
		subInfo = mMainActivity.dbm.getSubBillItems(mMainActivity.meetingId, bill.billId);
		aryContent = new ArrayList<ListItem>();

		if (bitNum == 0) {
			// 0 单字节数值(数值范围0-254) 现按一次最多显示5个事项
			fenmu = data[10] & 0xff;
			yindao = data[11] & 0xff;
			shidao = data[12] & 0xff;
			int pos = 13;
			for (int i = startNo; i <= endNo; i++) {
				ListItem it = new ListItem();
				it.No = i;
				it.title = getTitle(i);
				it.agree = data[pos++] & 0xff;
				it.oppose = data[pos++] & 0xff;
				if (pos == data.length - 1) {
					aryContent.add(it);
					break;
				}
				if (options.length == 3) {
					it.abstain = data[pos++] & 0xff;
				}
				if (pos == data.length - 1) {
					aryContent.add(it);
					break;
				}
				it.unvote = data[pos++] & 0xff;
				if (resultBits == 0xffff) {
//					it.pass = data[pos++] & 0xff;
					it.pass = 0xffff;
				} else {
					int moveBits = 15 - (i - startNo);
					it.pass = (resultBits >> moveBits) & 1;
				}
				aryContent.add(it);
			}
		} else if(bitNum == 1){
			// 1 双字节数值(数值范围0-65534) 现按一次最多显示5个事项
			fenmu = (data[10] << 8 | data[11]) & 0xffff;
			yindao = (data[12] << 8 | data[13]) & 0xffff;
			shidao = (data[14] << 8 | data[15]) & 0xffff;
			int pos = 16;
			for (int i = startNo; i <= endNo; i++) {
				ListItem it = new ListItem();
				it.No = i;
				it.title = getTitle(i);
				it.agree = (data[pos++] << 8 | data[pos++]) & 0xffff;
				it.oppose = (data[pos++] << 8 | data[pos++]) & 0xffff;
				if (options.length == 3) {
					it.abstain = (data[pos++] << 8 | data[pos++]) & 0xffff;
				}
				it.unvote = (data[pos++] << 8 | data[pos++]) & 0xffff;
				if (resultBits == 0xffff) {
					it.pass = 0xffff;
				} else {
					int moveBits = 15 - (i - startNo);
					it.pass = (resultBits >> moveBits) & 1;
				}
				aryContent.add(it);
			}
		}else if(bitNum == 2){
			// 0 单字节数值(数值范围0-254) 现按一次最多显示5个事项
			fenmu = data[10] & 0xff;
			yindao = data[11] & 0xff;
			shidao = data[12] & 0xff;
			int pos = 13;
			for (int i = startNo; i <= endNo; i++) {
				ListItem it = new ListItem();
				it.No = data[pos++] & 0xff;
				it.title = getTitle(it.No);
				it.agree = data[pos++] & 0xff;
				it.oppose = data[pos++] & 0xff;
				if (pos == data.length - 1) {
					aryContent.add(it);
					break;
				}
				if (options.length == 3) {
					it.abstain = data[pos++] & 0xff;
				}
				if (pos == data.length - 1) {
					aryContent.add(it);
					break;
				}
				it.unvote = data[pos++] & 0xff;
				if (resultBits == 0xffff) {
					it.pass = 0xffff;
				} else {
					int moveBits = 15 - (i - startNo);
					it.pass = (resultBits >> moveBits) & 1;
				}
				aryContent.add(it);
			}
		}else if(bitNum == 3){
			// 双字节数值制定表决事项编号(数值范围0-65534)现按一次最多显示5个事项
			// 单字节数值制定表决事项编号(数值范围0-254)现按一次最多显示5个事项
			// 1 双字节数值(数值范围0-65534) 现按一次最多显示5个事项
			fenmu = (data[10] << 8 | data[11]) & 0xffff;
			yindao = (data[12] << 8 | data[13]) & 0xffff;
			shidao = (data[14] << 8 | data[15]) & 0xffff;
			int pos = 16;
			for (int i = startNo; i <= endNo; i++) {
				ListItem it = new ListItem();
				it.No = (data[pos++] << 8 | data[pos++]) & 0xffff;
				it.title = getTitle(it.No);
				it.agree = (data[pos++] << 8 | data[pos++]) & 0xffff;
				it.oppose = (data[pos++] << 8 | data[pos++]) & 0xffff;
				if (options.length == 3) {
					it.abstain = (data[pos++] << 8 | data[pos++]) & 0xffff;
				}
				it.unvote = (data[pos++] << 8 | data[pos++]) & 0xffff;
				if (resultBits == 0xffff) {
					it.pass = 0xffff;
				} else {
					int moveBits = 15 - (i - startNo);
					it.pass = (resultBits >> moveBits) & 1;
				}
				aryContent.add(it);
			}
		}

	}

	private String getTitle(int billNo) {
		for (int i = 0; i < subInfo.size(); i++) {
			BillInfo subItem = subInfo.get(i);
			if (subItem.billNo == billNo) {
				return subItem.title;
			}
		}
		return "";
	}

	public class ListItem {
		int No;
		String title;
		int agree;
		int oppose;
		int abstain;
		int unvote;
		int pass;
	}

	/*
	 * MyAdapter
	 *
	 */
	private class MyAdapter extends BaseAdapter {

		private LayoutInflater mInflater;

		public MyAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return aryContent.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return aryContent.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public boolean isEnabled(int position) {
			// TODO Auto-generated method stub

			return super.isEnabled(position);
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			LogUtil.v("BaseAdapterTest", "getView " + position + " " + convertView);

			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.list_multi_vote_result_item, null);
				holder = new ViewHolder();

				holder.tvNum = (TextView) convertView.findViewById(R.id.list_multi_result_item_num);
				holder.tvTitle = (TextView) convertView.findViewById(R.id.list_multi_result_item_name);
				holder.tvAgree = (TextView) convertView.findViewById(R.id.list_multi_result_item_agree);
				holder.tvOppose = (TextView) convertView.findViewById(R.id.list_multi_result_item_oppose);
				holder.tvAbstain = (TextView) convertView.findViewById(R.id.list_multi_result_item_abstan);
				holder.tvUnvote = (TextView) convertView.findViewById(R.id.list_multi_result_item_unvote);
				holder.tvResult = (TextView) convertView.findViewById(R.id.list_multi_result_item_result);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}



			ListItem it = aryContent.get(position);
			holder.tvNum.setText(String.valueOf(it.No));
			holder.tvTitle.setText(it.title);


			holder.tvOppose.setText(String.valueOf(it.oppose));
			holder.tvAbstain.setText(String.valueOf(it.abstain));
			if(it.agree != 0xff && it.agree != 0xffff){
				holder.tvAgree.setVisibility(View.VISIBLE);
				holder.tvAgree.setText(String.valueOf(it.agree));
			}else{
				holder.tvAgree.setVisibility(View.INVISIBLE);
				holder.tvAgree.setText("");
			}
			if(it.oppose != 0xff && it.oppose != 0xffff){
				holder.tvOppose.setVisibility(View.VISIBLE);
				holder.tvOppose.setText(String.valueOf(it.oppose));
			}else{
				holder.tvOppose.setVisibility(View.INVISIBLE);
				holder.tvOppose.setText("");
			}
			if(it.abstain != 0xff && it.abstain != 0xffff){
				holder.tvAbstain.setVisibility(View.VISIBLE);
				holder.tvAbstain.setText(String.valueOf(it.abstain));
			}else{
				holder.tvAbstain.setVisibility(View.INVISIBLE);
				holder.tvAbstain.setText("");
			}

			if (it.unvote != 0xff && it.unvote != 0xffff) {
				titleUnvote.setVisibility(View.VISIBLE);
				holder.tvUnvote.setVisibility(View.VISIBLE);
				holder.tvUnvote.setText(String.valueOf(it.unvote));
			} else {
				titleUnvote.setVisibility(View.INVISIBLE);
				holder.tvUnvote.setVisibility(View.INVISIBLE);
				holder.tvUnvote.setText("");

			}

			String strPass = "";

			if (it.pass == 0) {
				strPass = getString(R.string.no_pass);
			} else if (it.pass == 1) {
				strPass = getString(R.string.pass);
			}

			holder.tvResult.setText(strPass);
			if (it.pass == 0xffff) {
				holder.tvResult.setVisibility(View.GONE);
			} else {
				holder.tvResult.setVisibility(View.VISIBLE);
			}

			if (options.length == 2) {
				holder.tvAbstain.setVisibility(View.GONE);

			} else {
				holder.tvAbstain.setVisibility(View.VISIBLE);
			}

			return convertView;
		}


		public final class ViewHolder {
			public TextView tvNum;
			public TextView tvTitle;
			public TextView tvAgree;
			public TextView tvOppose;
			public TextView tvAbstain;
			public TextView tvUnvote;
			public TextView tvResult;
		}
	}
}