2014-01-14 3 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) {부터 코드를 이동해야합니다. Checked의 값은 청취자를 지정할 때 (= 클릭 전에) 값을 확인하는 동안 클릭 한 후에 만 ​​변경됩니다.

0

는 "상태"의 체크 때 (JSON 데이터에 대한 u는 원하는이든 데이터) YES 또는 NO로 상태를 설정 게요 변수 또는 선택하지 않은이 있습니다이 코드

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

    }); 

을 시도

관련 문제