package com.sunvote.xpadapp.fragments; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.text.Html; 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.Button; import android.widget.ListView; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; 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 com.sunvote.xpadapp.dialog.ToastAlertDialog; import com.sunvote.xpadapp.utils.SharedPreferencesUtil; import com.sunvote.xpadcomm.XPadApi; import com.sunvote.xpadcomm.XPadApiInterface; import java.util.ArrayList; public class ElectionCustomFragment extends BaseFragment { private View view; private String TAG = "ElectionCustomFragment"; private ArrayList aryContent = null; //标题,标题选举信息,提示信息,结果,页码 private TextView tvTitle,tvTitleInfo,tvTipInfo,tvResult,tvSubmiting; private ListView listview; private Button btnConfirm; private Button btnModify; private View btnPageUp; private View btnPageDown; private View btnPageLast; private Button btnConfirmOK; private Button btnConfirmCancel; //分页 private final int pageSize = 6; private int page = 1; private int totalPage; //赞成\反对\弃权票数 private int agreeVotedCount; private int opposeVotedCount; private int abstantVotedCount; //数据适配器 private MyAdapter mAdapter; //投票信标 private XPadApiInterface.VoteInfo voteInfo; //基础信标 private BillInfo bill; //确认面板 private RelativeLayout confirm_panel; private View resultLayout; public TextView confirm_tv; public void setInfo(BillInfo info, ArrayList subInfo, XPadApiInterface.VoteInfo voteInfo){ this.bill = info; this.aryContent = subInfo; this.voteInfo=voteInfo; } @SuppressLint("NewApi") @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d(TAG, "onCreateView"); view = inflater.inflate(R.layout.fragment_custom_election, container, false); initView(); initData(); initEvent(); return view; } private void initData(){ totalPage = aryContent.size() / pageSize; if (aryContent.size() % pageSize > 0) { totalPage++; } listview.setFastScrollAlwaysVisible(false); mAdapter = new MyAdapter(getActivity()); listview.setAdapter(mAdapter); //显示页 if(voteInfo!=null){ tvTitle.setText(bill.title); showVote(); checkVoted(); checkIsVoted(); } else{ hideVote(); } } private void initEvent(){ view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ; } }); confirm_panel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ; } }); btnPageUp.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (page >= 1) { page--; listview.setSelection(page * pageSize); } } }); btnPageDown.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (page < totalPage) { page++; listview.setSelection((page) * pageSize); if(page == totalPage){ page--; } } } }); btnPageLast.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { page=totalPage; listview.setSelection(aryContent.size()); } }); btnConfirm.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(voteInfo.mode2_modify==0){ showConfirm(); confirm_tv.setText("可选"+ voteInfo.limitFavor +"项,已选"+agreeVotedCount+"项,确定提交吗?"); }else{ hideConfirm(); confirmInfo(); } } }); btnConfirmOK.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { hideConfirm(); confirmInfo(); } }); btnConfirmCancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { listview.setVisibility(View.VISIBLE); hideConfirm(); hideResult(); showVote(); } }); btnModify.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { for (int i = 0; i < aryContent.size(); i++) { MultiTitleItem it = aryContent.get(i); it.modifyModel = false; if(it.No<0){ it.result=1; } } btnConfirm.setEnabled(true); btnConfirm.setVisibility(View.VISIBLE); btnModify.setVisibility(View.INVISIBLE); listview.setVisibility(View.VISIBLE); hideResult(); checkVoted(); } }); } private void initView(){ listview = (ListView) view.findViewById(R.id.election_listview); tvTitle = (TextView) view.findViewById(R.id.election_title); tvTitleInfo= (TextView) view.findViewById(R.id.title_info); tvTipInfo= (TextView) view.findViewById(R.id.tv_tip_info); tvResult =(TextView) view.findViewById(R.id.singlevote_tv_result); tvSubmiting = (TextView) view.findViewById(R.id.submiting_tv); resultLayout =(View) view.findViewById(R.id.result_layout); confirm_panel = (RelativeLayout) view.findViewById(R.id.singlevote_confirm_panel); btnPageUp = view.findViewById(R.id.election_pageup); btnPageDown = view.findViewById(R.id.election_pagedown); btnPageLast = view.findViewById(R.id.election_pagelast); btnConfirm = (Button) view.findViewById(R.id.election_btn_confirm); btnModify = (Button)view.findViewById(R.id.election_btn_modify); btnConfirmOK = (Button) view.findViewById(R.id.singlevote_btn_confirm_ok); btnConfirmCancel = (Button) view.findViewById(R.id.singlevote_btn_confirm_cancel); confirm_tv = (TextView) view.findViewById(R.id.singlevote_confirm_textview); } private void confirmInfo(){ btnConfirm.setEnabled(false); if (agreeVotedCount < voteInfo.limitFavor) { ToastAlertDialog.makeText(mMainActivity, "需投票"+voteInfo.limitFavor+"张").show(); btnConfirm.setEnabled(true); return; } /* int count = agreeVotedCount+opposeVotedCount+abstantVotedCount; if(count= voteInfo.limitFavor){ holder.btnO.setChecked(false); ToastAlertDialog.makeText(mMainActivity, "已投"+agreeVotedCount+getString(R.string.fix)+",不可多选").show(); checkVoted(); return; } voteWithValue(position, 1); } } }); holder.btnX.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { MultiTitleItem it = aryContent.get(position); if(it.result == 2){ voteWithValue(position,0); }else{ voteWithValue(position, 2); } } }); holder.btnAbstain.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { MultiTitleItem it = aryContent.get(position); if(it.result == 3){ voteWithValue(position,0); }else{ voteWithValue(position, 3); } } }); return convertView; } public final class ViewHolder { public TextView tvNum; public TextView tvName; public RadioButton btnO; public RadioButton btnX; public RadioButton btnAbstain; public RadioGroup RG; } } /** * 投票 * @param pos * @param val */ private void voteWithValue(int pos, final int val) { final MultiTitleItem it = aryContent.get(pos); it.result = val; if(val==0){ it.result = 4; } mMainActivity.presenter.submitVote(XPadApi.AnsType_BatchSingle, it.No + ":" + val); checkVoted(); } private void showResult(){ //tvResult.setText("已投票"); tvResult.setVisibility(View.VISIBLE); resultLayout.setVisibility(View.VISIBLE); tvSubmiting.setVisibility(View.GONE); this.listview.setEnabled(false); } private void hideResult(){ //tvResult.setText("已投票"); tvResult.setVisibility(View.GONE); resultLayout.setVisibility(View.INVISIBLE); tvSubmiting.setVisibility(View.GONE); this.listview.setEnabled(true); } private void checkVoted() { agreeVotedCount = 0; opposeVotedCount = 0; abstantVotedCount = 0; for (int i = 0; i < aryContent.size(); i++) { MultiTitleItem it = aryContent.get(i); if (it.result == 1) { agreeVotedCount++; } if(it.result == 2){ opposeVotedCount++; } if(it.result == 3){ abstantVotedCount++; } } if(voteInfo!=null){ //tvTitleInfo.setText(getString(R.string.agree_vote_ok) + voteInfo.limitFavor+ getString(R.string.fix) +",已投"+ agreeVotedCount + getString(R.string.fix)); String str= String.format("%s%s%s,已投%s%s",getString(R.string.agree_vote_ok), voteInfo.limitFavor,getString(R.string.fix),agreeVotedCount,getString(R.string.fix)); tvTitleInfo.setText(Html.fromHtml(str)); } mAdapter.notifyDataSetChanged(); listview.invalidate(); } String voteValues; private void saveVoteInfo(){ StringBuilder values = new StringBuilder(""); for(int i=0;i0){ if(ary.length == aryContent.size()){ for(int i=0;i