ResultVoteFragment.java 13.1 KB
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.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
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.xpadcomm.XPadApiInterface;

import org.apache.tools.ant.Main;

import java.text.DecimalFormat;

public class ResultVoteFragment extends BaseFragment {

    private static String TAG = ResultVoteFragment.class.getSimpleName();
    private XPadApiInterface.VoteInfo voteInfo;
    private TextView yingdaoresult;
    private TextView shidaoresult;
    // 表決结果
    private TextView fragmentResultMemo;
    private TextView fragmentMemo;
    //
    private RelativeLayout dataTitleLayout1;
    private RelativeLayout dataTitleLayout2;
    private RelativeLayout dataTitleLayout3;
    private RelativeLayout dataTitleLayout4;
//    private LinearLayout dResult;
//    private LinearLayout pResult;

    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);
        }
    }

    public void setVoteInfo(XPadApiInterface.VoteInfo voteInfo) {
        this.voteInfo = voteInfo;
    }

    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;
    }

    @Override
    public void onResume() {
        super.onResume();
        ((MainActivity)getActivity()).setBackgroundColor(Color.parseColor("#042148"));
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_result_vote, container, false);
        root.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        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);

        dataTitleLayout1 = root.findViewById(R.id.data_title_layout1);
        dataTitleLayout2 = root.findViewById(R.id.data_title_layout2);
        dataTitleLayout3 = root.findViewById(R.id.data_title_layout3);
        dataTitleLayout4 = root.findViewById(R.id.data_title_layout4);

        showResult();
        return root;
    }

    private void showResult() {
        int pass = voteInfo.resultInfo.bits & 0xF;
        int xiaoShuWei = (voteInfo.resultInfo.bits >> 4) & 0xF;
        int fenmu = voteInfo.resultInfo.num0;
        LogUtil.d(TAG, "xiaoshu:" + xiaoShuWei + "  fenmu:" + fenmu);
        if (voteInfo.resultInfo.num1 != 0xffff) {
            yingdaoresult.setText("" + voteInfo.resultInfo.num1);
        }
        if (voteInfo.resultInfo.num2 != 0xffff) {
            shidaoresult.setText("" + voteInfo.resultInfo.num2);
        }
        if (pass == 0xf) {
            fragmentResultMemo.setText("");
        } else if (pass == 0) {
            fragmentResultMemo.setText(getString(R.string.no_pass));
        } else {
            fragmentResultMemo.setText(getString(R.string.pass));
        }

        LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT,1.0f);
        if (voteInfo.resultInfo.resultType == 1) {
            fragmentMemo.setText(getString(R.string.biaojue_result));
            String szc = "" ;
            String sfd = "" ;
            String swa = "" ;
            if (fenmu > 0 && fenmu != 0xffff) {
                double pzc = voteInfo.resultInfo.num4 * 1.0 / fenmu * 100;
                szc = formatDoubleToString(pzc, xiaoShuWei, false);
                double pfd = voteInfo.resultInfo.num5 * 1.0 / fenmu * 100;
                sfd = formatDoubleToString(pfd, xiaoShuWei, false);
                double pwa = voteInfo.resultInfo.num3 * 1.0 / fenmu * 100;
                swa = formatDoubleToString(pwa, xiaoShuWei, false);
            }
            if (voteInfo.resultInfo.num4 != 0xffff) {
                dataTitleLayout1.setVisibility(View.VISIBLE);
                String text = "" + voteInfo.resultInfo.num4 ;
                if(szc != null && !"".equals(szc)){
                    text += "(" + szc + "%)" ;
                }
                ((TextView)(dataTitleLayout1.findViewById(R.id.data_title_layout1_text1))).setText(getString(R.string.agree));
                ((TextView)(dataTitleLayout1.findViewById(R.id.data_title_layout1_text2))).setText(text);
            }
            if (voteInfo.resultInfo.num5 != 0xffff) {
                dataTitleLayout2.setVisibility(View.VISIBLE);
                String text = "" + voteInfo.resultInfo.num5 ;
                if(sfd != null && !"".equals(sfd)){
                    text += "(" + sfd + "%)" ;
                }
                ((TextView)(dataTitleLayout2.findViewById(R.id.data_title_layout2_text1))).setText(getString(R.string.oppose));
                ((TextView)(dataTitleLayout2.findViewById(R.id.data_title_layout2_text2))).setText(text);
            }
            if (voteInfo.resultInfo.num3 != 0xffff) {
                dataTitleLayout3.setVisibility(View.VISIBLE);
                String text = "" + voteInfo.resultInfo.num3 ;
                if(swa != null && !"".equals(swa)){
                    text += "(" + swa + "%)" ;
                }
                ((TextView)(dataTitleLayout3.findViewById(R.id.data_title_layout3_text1))).setText(getString(R.string.weian));
                ((TextView)(dataTitleLayout3.findViewById(R.id.data_title_layout3_text2))).setText(text);
            }
        } else {
            String szc = "";
            String sfd = "";
            String swa = "";
            if (fenmu > 0 && fenmu != 0xffff) {
                double pzc = voteInfo.resultInfo.num4 * 1.0 / fenmu * 100;
                szc = formatDoubleToString(pzc, xiaoShuWei, false);
                double pfd = voteInfo.resultInfo.num5 * 1.0 / fenmu * 100;
                sfd = formatDoubleToString(pfd, xiaoShuWei, false);
                double pwa = voteInfo.resultInfo.num6 * 1.0 / fenmu * 100;
                swa = formatDoubleToString(pwa, xiaoShuWei, false);
            }
            if (voteInfo.resultInfo.num4 != 0xffff) {

                dataTitleLayout1.setVisibility(View.VISIBLE);
                String text = "" + voteInfo.resultInfo.num4 ;
                if(szc != null && !"".equals(szc)){
                    text += "(" + szc + "%)" ;
                }
                ((TextView)(dataTitleLayout1.findViewById(R.id.data_title_layout1_text2))).setText(text);
            }
            if (voteInfo.resultInfo.num5 != 0xffff) {
                dataTitleLayout2.setVisibility(View.VISIBLE);
                String text = "" + voteInfo.resultInfo.num5 ;
                if(sfd != null && !"".equals(sfd)){
                    text += "(" + sfd + "%)" ;
                }
                ((TextView)(dataTitleLayout2.findViewById(R.id.data_title_layout2_text2))).setText(text);
            }
            if (voteInfo.resultInfo.num6 != 0xffff) {
                dataTitleLayout1.setVisibility(View.VISIBLE);
                String text = "" + voteInfo.resultInfo.num6 ;
                if(swa != null && !"".equals(swa)){
                    text += "(" + swa + "%)" ;
                }
                ((TextView)(dataTitleLayout3.findViewById(R.id.data_title_layout3_text2))).setText(text);
            }
            if (voteInfo.resultInfo.resultType == 2) {
                fragmentMemo.setText(getString(R.string.biaojue_result));
                if (voteInfo.resultInfo.num4 != 0xffff) {
                    dataTitleLayout1.setVisibility(View.VISIBLE);
                    ((TextView)(dataTitleLayout1.findViewById(R.id.data_title_layout1_text1))).setText(getString(R.string.agree));
                }
                if (voteInfo.resultInfo.num5 != 0xffff) {
                    dataTitleLayout2.setVisibility(View.VISIBLE);
                ((TextView)(dataTitleLayout2.findViewById(R.id.data_title_layout2_text1))).setText(getString(R.string.oppose));
                }
                if (voteInfo.resultInfo.num6 != 0xffff) {
                    dataTitleLayout3.setVisibility(View.VISIBLE);
                    ((TextView)(dataTitleLayout3.findViewById(R.id.data_title_layout3_text1))).setText(getString(R.string.abstant));
                }
                if (pass == 0) {
                    fragmentResultMemo.setText(getString(R.string.no_pass));
                } else {
                    fragmentResultMemo.setText(getString(R.string.pass));
                }
            } else if (voteInfo.resultInfo.resultType == 4) {
                fragmentMemo.setText(getString(R.string.ceping_result));
                if (voteInfo.resultInfo.num4 != 0xffff) {
                    dataTitleLayout1.setVisibility(View.VISIBLE);
                    ((TextView)(dataTitleLayout1.findViewById(R.id.data_title_layout1_text1))).setText(getString(R.string.manyi));
                }
                if (voteInfo.resultInfo.num5 != 0xffff) {
                    dataTitleLayout2.setVisibility(View.VISIBLE);
                    ((TextView)(dataTitleLayout2.findViewById(R.id.data_title_layout2_text1))).setText(getString(R.string.jbmanyi));
                }
                if (voteInfo.resultInfo.num6 != 0xffff) {
                    dataTitleLayout3.setVisibility(View.VISIBLE);
                    ((TextView)(dataTitleLayout3.findViewById(R.id.data_title_layout3_text1))).setText(getString(R.string.bumanyi));
                }
                if (pass == 0) {
                    fragmentResultMemo.setText(getString(R.string.manyi));
                } else if (pass == 1) {
                    fragmentResultMemo.setText(getString(R.string.jbmanyi));
                } else {
                    fragmentResultMemo.setText(getString(R.string.bumanyi));
                }
            } else if (voteInfo.resultInfo.resultType == 12) {
                fragmentMemo.setText(getString(R.string.ceping_result));
                if (voteInfo.resultInfo.num4 != 0xffff) {
                    dataTitleLayout1.setVisibility(View.VISIBLE);
                    ((TextView)(dataTitleLayout1.findViewById(R.id.data_title_layout1_text1))).setText(getString(R.string.manyi));
                }
                if (voteInfo.resultInfo.num5 != 0xffff) {
                    dataTitleLayout2.setVisibility(View.VISIBLE);
                    ((TextView)(dataTitleLayout2.findViewById(R.id.data_title_layout2_text1))).setText(getString(R.string.bumanyi));
                }
                if (voteInfo.resultInfo.num6 != 0xffff) {
                    dataTitleLayout3.setVisibility(View.VISIBLE);
                    ((TextView)(dataTitleLayout3.findViewById(R.id.data_title_layout3_text1))).setText(getString(R.string.fcbumanyi));
                }
                if (pass == 0) {
                    fragmentResultMemo.setText(getString(R.string.manyi));
                } else if (pass == 1) {
                    fragmentResultMemo.setText(getString(R.string.bumanyi));
                } else {
                    fragmentResultMemo.setText(getString(R.string.fcbumanyi));
                }
            }
            if(voteInfo.resultInfo.num3 != 0xffff){
                double opt4 = voteInfo.resultInfo.num3 * 1.0 / fenmu * 100;
                String strOpt4 = formatDoubleToString(opt4, xiaoShuWei, false);
                dataTitleLayout4.setVisibility(View.VISIBLE);
                ((TextView)(dataTitleLayout4.findViewById(R.id.data_title_layout4_text1))).setText(getString(R.string.weian));
                String text = "" + voteInfo.resultInfo.num3 ;
                if(strOpt4 != null && !"".equals(strOpt4)){
                    text += "(" + strOpt4 + "%)" ;
                }
                ((TextView)(dataTitleLayout4.findViewById(R.id.data_title_layout4_text2))).setText(text);
            }
        }
        if (pass == 0xf) {
            fragmentResultMemo.setText("");
        }
     }
}