如何在方向更改时选择Checkbox时停止获取数据

时间:2014-08-12 05:24:24

标签: android checkbox broadcastreceiver fragment

在我的屏幕中,我显示的复选框是响应时使用GET 0表示setchecked(false),当收到1表示setchecked(true)。

问题:

  

考虑我何时被选中多个复选框并更改了我的   方向片段正在重新加载,它绑定数据   GET方法。如何克服这个问题。我发布了我的代码   下面。

 @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        if(savedInstanceState!=null)
        {

            cBox_furnishing.setChecked(savedInstanceState.getBoolean("Furnish"));
            cBox_washMachine.setChecked(savedInstanceState.getBoolean("washMachine"));
            cBox_ac.setChecked(savedInstanceState.getBoolean("ac"));
            cBox_microwaveOven.setChecked(savedInstanceState.getBoolean("microwaveOven"));
            cBox_ctv.setChecked(savedInstanceState.getBoolean("ctv"));
            cBox_refrigerator.setChecked(savedInstanceState.getBoolean("refrigerator"));
            cBox_tw.setChecked(savedInstanceState.getBoolean("tw"));
            cBox_music.setChecked(savedInstanceState.getBoolean("music"));
            cBox_pc.setChecked(savedInstanceState.getBoolean("pc"));
            cBox_car.setChecked(savedInstanceState.getBoolean("car"));

       }
        Fragment_resident_info.leadno = getArguments().getString("LeadNo");    
        getdata();
    }



      @Override
          public void onSaveInstanceState(Bundle savedInstanceState) 
          {

              b_furnishing_CHECK = cBox_furnishing.isChecked();
              b_washMachine_CHECK = cBox_washMachine.isChecked();
              b_ac_CHECK = cBox_ac.isChecked();
              b_microwaveOven_CHECK = cBox_microwaveOven.isChecked();
              b_ctv_CHECK = cBox_ctv.isChecked();
              b_refrigerator_CHECK = cBox_refrigerator.isChecked();
              b_tw_CHECK = cBox_tw.isChecked();
              b_music_CHECK = cBox_music.isChecked();
              b_pc_CHECK = cBox_pc.isChecked();
              b_car_CHECK = cBox_car.isChecked();

              savedInstanceState.putBoolean("Furnish", b_furnishing_CHECK);
              savedInstanceState.putBoolean("washMachine", b_washMachine_CHECK);
              savedInstanceState.putBoolean("ac", b_ac_CHECK);
              savedInstanceState.putBoolean("microwaveOven", b_microwaveOven_CHECK);
              savedInstanceState.putBoolean("ctv", b_ctv_CHECK);
              savedInstanceState.putBoolean("refrigerator", b_refrigerator_CHECK);
              savedInstanceState.putBoolean("tw", b_tw_CHECK);
              savedInstanceState.putBoolean("music", b_music_CHECK);
              savedInstanceState.putBoolean("pc", b_pc_CHECK);
              savedInstanceState.putBoolean("car", b_car_CHECK);


              super.onSaveInstanceState(savedInstanceState);
          } 



    private BroadcastReceiver reciever_assetseen = new BroadcastReceiver() 
    {
        @Override
        public void onReceive(Context context, Intent intent) {
            String response = intent.getStringExtra("httpResponse");
            try
            {
                JSONArray ary = new JSONArray(response);
                JSONObject obj = ary.getJSONObject(0);
                String st_furnishing = obj.getString("Residence_Furnish");
                String st_washingmachine = obj.getString("Washing_Machine");
                String st_Ac = obj.getString("AC");
                String st_microwaveoven = obj.getString("Microwave_Oven");
                String st_ctv = obj.getString("CTV");
                String st_refridge = obj.getString("Refrigerator");
                String st_tw = obj.getString("TW");
                String st_music = obj.getString("Music");
                String st_pc = obj.getString("PC");
                String st_car = obj.getString("CAR");

                if(b_furnishing_CHECK == true)
                {
                    cBox_furnishing.setChecked(true);
                }
                else if(st_furnishing.equals("0") || st_furnishing.equals("null")  )
                    {
                        cBox_furnishing.setChecked(false);
                    }

                else
                {
                    cBox_furnishing.setChecked(true);
                }
                if(b_washMachine_CHECK == true)
                {
                    cBox_washMachine.setChecked(true);
                }

                else if(st_washingmachine.equals("0") || st_washingmachine.equals("null") )
                {
                    cBox_washMachine.setChecked(false);
                }

                else
                {
                    cBox_washMachine.setChecked(true);
                }
                if(  b_ac_CHECK == true)
                {
                    cBox_ac.setChecked(true);
                }

                else if(st_Ac.equals("0")|| st_Ac.equals("null"))
                {
                    cBox_ac.setChecked(false);
                }
                else
                {
                    cBox_ac.setChecked(true);
                }
                if(  b_microwaveOven_CHECK == true)
                {
                    cBox_microwaveOven.setChecked(true);
                }
                 else if(st_microwaveoven.equals("0") || st_microwaveoven.equals("null") )
                {
                    cBox_microwaveOven.setChecked(false);
                }

                else
                {
                    cBox_microwaveOven.setChecked(true);
                }
                  if(  b_ctv_CHECK == true)
                    {
                        cBox_ctv.setChecked(true);
                    }
                  else if(st_ctv.equals("0")|| st_ctv.equals("null") )
                {
                    cBox_ctv.setChecked(false);
                }

                else
                {
                    cBox_ctv.setChecked(true);
                }
                   if(  b_refrigerator_CHECK == true)
                    {
                        cBox_refrigerator.setChecked(true);
                    }
                   else if(st_refridge.equals("0")|| st_refridge.equals("null") )
                {
                    cBox_refrigerator.setChecked(false);
                }

                else
                {
                    cBox_refrigerator.setChecked(true);
                }

                 if(  b_tw_CHECK == true)
                {
                    cBox_tw.setChecked(true);
                }
                 else if(st_tw.equals("0")|| st_tw.equals("null") )
                {
                    cBox_tw.setChecked(false);
                }
                else
                {
                    cBox_tw.setChecked(true);
                }

                 if(  b_music_CHECK == true)
                {
                    cBox_music.setChecked(true);
                }
                 else if(st_music.equals("0")|| st_music.equals("null"))
                {
                    cBox_music.setChecked(false);
                }
                else
                {
                    cBox_music.setChecked(true);
                }

                 if(  b_pc_CHECK == true)
                {
                    cBox_pc.setChecked(true);
                }
                 else if(st_pc.equals("0")|| st_pc.equals("null") )
                {
                    cBox_pc.setChecked(false);
                }
                else
                {
                    cBox_pc.setChecked(true);
                }

                 if(  b_car_CHECK == true)
                {
                    cBox_car.setChecked(true);
                }
                 else if(st_car.equals("0")|| st_car.equals("null"))
                {
                    cBox_car.setChecked(false);
                }
                else
                {
                    cBox_car.setChecked(true);
                }
            }
            catch(Exception e)
            {

            }
        }
    };

1 个答案:

答案 0 :(得分:0)

if(savedInstanceState!=null)
        {

            cBox_furnishing.setChecked(savedInstanceState.getBoolean("Furnish"));
            cBox_washMachine.setChecked(savedInstanceState.getBoolean("washMachine"));
            cBox_ac.setChecked(savedInstanceState.getBoolean("ac"));
            cBox_microwaveOven.setChecked(savedInstanceState.getBoolean("microwaveOven"));
            cBox_ctv.setChecked(savedInstanceState.getBoolean("ctv"));
            cBox_refrigerator.setChecked(savedInstanceState.getBoolean("refrigerator"));
            cBox_tw.setChecked(savedInstanceState.getBoolean("tw"));
            cBox_music.setChecked(savedInstanceState.getBoolean("music"));
            cBox_pc.setChecked(savedInstanceState.getBoolean("pc"));
            cBox_car.setChecked(savedInstanceState.getBoolean("car"));

       }else{

        Fragment_resident_info.leadno = getArguments().getString("LeadNo");    
        getdata();
}