2014-01-14 68 views
0

我正在爲學校建築中的機制創建一個Android應用程序。他們需要掃描一個房間的QR碼,以查看課室內是否有任何東西被破壞(比如需要修復的投影儀)。解決此問題後,他們可以將複選框設置爲true,以讓其他用戶知道此作業已完成。保存對JSON文件的更改Android

我正在使用json文件來獲取我的信息,所以當用戶選中複選框時,我的json文件需要更新..我試了很多,但沒有找到解決方案。

有人可以幫我嗎?

這是我所知道..

finished.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
         @Override 
         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
          if (isChecked) { 
           checked = true; 
          } else { 
           checked = false; 
          } 
         } 
        }); 

        if (checked) { 
         //JSONObject modified = new JSONObject(); 
         try { 
          JSONArray d = new JSONArray(); 
          JSONObject tour; 

          tour = new JSONObject(); 
          tour.put("done" + (i + 1), "Yes"); 
          d.put(tour); 

          String text = data.toString(); 
          FileOutputStream fos = openFileOutput("tours", MODE_PRIVATE); 
          fos.write(text.getBytes()); 
          fos.close(); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } catch (FileNotFoundException e) { 
          e.printStackTrace(); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
        } else { 
         //JSONObject modified = new JSONObject(); 
         try { 
          JSONArray d = new JSONArray(); 
          JSONObject tour; 

          tour = new JSONObject(); 
          tour.put("done" + (i + 1), "No"); 
          d.put(tour); 

          String text = data.toString(); 
          FileOutputStream fos = openFileOutput("tours", MODE_PRIVATE); 
          fos.write(text.getBytes()); 
          fos.close(); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } catch (FileNotFoundException e) { 
          e.printStackTrace(); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
        } 

回答

0

你將不得不擺脫if (checked) {在監聽器裏或到您從聽衆中調用的方法啓動代碼。選中的值只有在點擊後纔會更改,而您在分配偵聽器時檢查該值(=點擊之前)。

0

試試這個代碼

finished.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, 
       boolean isChecked) { 
      String status = "No"; 
      if (isChecked) { 
       status = "Yes"; 
      } 
       // JSONObject modified = new JSONObject(); 
       try { 
        JSONArray d = new JSONArray(); 
        JSONObject tour; 

        tour = new JSONObject(); 
        tour.put("done" + (i + 1), status); 
        d.put(tour); 

        String text = data.toString(); 
        FileOutputStream fos = openFileOutput("tours", 
          MODE_PRIVATE); 
        fos.write(text.getBytes()); 
        fos.close(); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } else { 
       // JSONObject modified = new JSONObject(); 
       try { 
        JSONArray d = new JSONArray(); 
        JSONObject tour; 

        tour = new JSONObject(); 
        tour.put("done" + (i + 1), "No"); 
        d.put(tour); 

        String text = data.toString(); 
        FileOutputStream fos = openFileOutput("tours", 
          MODE_PRIVATE); 
        fos.write(text.getBytes()); 
        fos.close(); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 

    }); 

有一個「狀態」變量,它會將狀態設置爲YES或NO(或任何數據u想爲JSON數據),其檢查時或取消選中