2012-09-16 5 views
0

현재 4 명의 플레이어의 스코어 카드를 작업 중이며 사용자가 페이지 하단의 4 명의 플레이어에 대한 점수를 입력하고 + 버튼을 누르면 입력 한 점수가 위의 스크롤 뷰.android 스코어 카드 - sharedpreferences and display

슬롯 번호는 사용자가 해당 EditText에 1, 12, 34, 56, 78로 입력 할 때 앱이 slot = 1에 대한 행을 인식하고 추가 할 수 있도록 라운드 번호로 정의됩니다. 플레이어 2는 34, P3 = 56, P4 = 78이 될 것입니다.

그런 다음 사용자가 2, 23, 45, 67, 89와 같은 다른 세트를 입력하면 라운드 2와 같이 추가됩니다. 지금까지 행을 올바르게 추가 할 수 있지만 슬롯 번호는 1, 2, 3 등이 될 수 있지만 이유는 모르겠 음

1 : 슬롯 번호는 1로 정확하지만 모든 4 명의 플레이어 점수 당신은 모든에 대해 동일한 SharedPrefs 및 키를 사용하고 2와 올바른 슬롯 번호 만 입력으로 모든 4 개 플레이어 점수는 위의 89

public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // get the SharedPreferences that contains the user's saved slots 
     SavedSlotsP1 = getSharedPreferences("slots", MODE_PRIVATE); 
     SavedSlotsP2 = getSharedPreferences("slots", MODE_PRIVATE); 
     SavedSlotsP3 = getSharedPreferences("slots", MODE_PRIVATE); 
     SavedSlotsP4 = getSharedPreferences("slots", MODE_PRIVATE); 

     SlotTableLayout = (TableLayout) findViewById(R.id.SlotTableLayout); 
     SlotEditText = (EditText) findViewById(R.id.SlotEditText); 
     P1ScoreEditText = (EditText) findViewById(R.id.P1ScoreEditText); 
     P2ScoreEditText = (EditText) findViewById(R.id.P2ScoreEditText); 
     P3ScoreEditText = (EditText) findViewById(R.id.P3ScoreEditText); 
     P4ScoreEditText = (EditText) findViewById(R.id.P4ScoreEditText); 

     refreshButtons(null); 
    } // end method onCreate 

public OnClickListener addButtonListener = new OnClickListener() 
    // create a new Button and add it to the ScrollView 
    { 
     @Override 
     public void onClick(View v) 
     { 
     if (SlotEditText.getText().length() > 0 && 
      P1ScoreEditText.getText().length() > 0 && 
      P2ScoreEditText.getText().length() > 0 && 
      P3ScoreEditText.getText().length() > 0 && 
      P4ScoreEditText.getText().length() > 0)    
     { 
      SaveTagToFile(SlotEditText.getText().toString(), 
        P1ScoreEditText.getText().toString(), 
        P2ScoreEditText.getText().toString(), 
        P3ScoreEditText.getText().toString(), 
        P4ScoreEditText.getText().toString()); 

      SlotEditText.setText(""); 
      P1ScoreEditText.setText(""); 
      P2ScoreEditText.setText(""); 
      P3ScoreEditText.setText(""); 
      P4ScoreEditText.setText(""); 

      // hide the soft keyboard 
      ((InputMethodManager) getSystemService(
       Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
       SlotEditText.getWindowToken(), 0); 
     } // end if 
     else 
     { 
      AlertDialog.Builder builder = new AlertDialog.Builder(FavoriteTwitterSearches.this); 
      builder.setTitle(R.string.missingTitle); 
      builder.setMessage(R.string.missingMessage); 
      builder.setPositiveButton(R.string.OK, null);    
      AlertDialog errorDialog = builder.create(); 
      errorDialog.show(); 
     } 
     } 
    };  

    private void SaveTagToFile(String Slot, String P1Score, String P2Score, String P3Score, String P4Score) 
    // save the new row to the file, then refresh all Buttons 
    { 
     // originalScore will be null if we're modifying an existing search 
     String originalScoreP1 = SavedSlotsP1.getString(Slot, null); 
     String originalScoreP2 = SavedSlotsP2.getString(Slot, null); 
     String originalScoreP3 = SavedSlotsP3.getString(Slot, null); 
     String originalScoreP4 = SavedSlotsP4.getString(Slot, null); 

     // get a SharedPreferences.Editor to store new row data 
     SharedPreferences.Editor preferencesEditorP1 = SavedSlotsP1.edit(); 
     SharedPreferences.Editor preferencesEditorP2 = SavedSlotsP2.edit(); 
     SharedPreferences.Editor preferencesEditorP3 = SavedSlotsP3.edit(); 
     SharedPreferences.Editor preferencesEditorP4 = SavedSlotsP4.edit(); 

     preferencesEditorP1.putString(Slot, P1Score); 
     preferencesEditorP2.putString(Slot, P2Score); 
     preferencesEditorP3.putString(Slot, P3Score); 
     preferencesEditorP4.putString(Slot, P4Score); 

     preferencesEditorP1.apply(); 
     preferencesEditorP2.apply(); 
     preferencesEditorP3.apply(); 
     preferencesEditorP4.apply(); 

     // if this is a new slot, add its GUI 
     if (originalScoreP1 == null) //P1 imply also P2, P3, P4 
      refreshButtons(Slot); 
    } 

private void refreshButtons(String ThereIsNewSlot) 
    // recreate search tag and edit Buttons for all saved searches; 
    // pass null in all circumstances to renew and recreate to show all the saved rows 

    { 
     // store saved tags in the tags array 
     String[] slots = SavedSlotsP1.getAll().keySet().toArray(new String[0]); 

     Arrays.sort(slots, String.CASE_INSENSITIVE_ORDER); // sort by slot 

     // if a new row was added, insert in GUI at the appropriate location 
     if (ThereIsNewSlot != null) {ToDisplayTagGUI(ThereIsNewSlot, Arrays.binarySearch(slots, ThereIsNewSlot));} 
     else // recreate and display GUI for ALL tags 
     {for (int index = 0; index < slots.length; ++index) ToDisplayTagGUI(slots[index], index);} 
    } 

    private void ToDisplayTagGUI(String Slot, int index) 
    // add a new tag button and corresponding edit button to the GUI 
    { 
     LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View newTagView = inflater.inflate(R.layout.new_tag_view, null); 

     EditText SlotNewTagEditText = (EditText) newTagView.findViewById(R.id.SlotNewTagEditText); 
     EditText P1NewTagScoreEditText = (EditText) newTagView.findViewById(R.id.P1NewTagScoreEditText); 
     EditText P2NewTagScoreEditText = (EditText) newTagView.findViewById(R.id.P2NewTagScoreEditText); 
     EditText P3NewTagScoreEditText = (EditText) newTagView.findViewById(R.id.P3NewTagScoreEditText); 
     EditText P4NewTagScoreEditText = (EditText) newTagView.findViewById(R.id.P4NewTagScoreEditText); 
     Button newTagEditButton = (Button) newTagView.findViewById(R.id.NewTagEditButton); 

     String P1Score = SavedSlotsP1.getString(Slot, ""); 
     String P2Score = SavedSlotsP2.getString(Slot, ""); 
     String P3Score = SavedSlotsP3.getString(Slot, ""); 
     String P4Score = SavedSlotsP4.getString(Slot, ""); 

     SlotNewTagEditText.setText(""+Slot); // assume Slot for P1 = P2 = P3 = P4 
     P1NewTagScoreEditText.setText(""+P1Score); 
     P2NewTagScoreEditText.setText(""+P2Score); 
     P3NewTagScoreEditText.setText(""+P3Score); 
     P4NewTagScoreEditText.setText(""+P4Score); 

     // add new tag and edit buttons to queryTableLayout 
     SlotTableLayout.addView(newTagView, index); 
    } 

답변

0

될 : 입력으로 위의 2 라운드를 위해 78,

될 플레이어의 점수이므로 각 연속 기록에서 무시됩니다.

각 플레이어에 대해로드 된 환경 설정을 변경하거나 키를 고유하게 설정하십시오. 나는 후자를 만들 것입니다. 즉, 각 플레이어의 매장 개념을 버리고 다음과 같이 할 수 있습니다.

scoreStore = getSharedPreferences("scores", MODE_PRIVATE); 

... 

scoreEditor = scoreStore.edit(); 
scoreEditor.putString("p1score", p1score); 
scoreEditor.putString("p2score", p2score); 
scoreEditor.putString("p3score", p3score); 
scoreEditor.putString("p4score", p4score); 
scoreEditor.apply(); 
+0

감사합니다! 나는 당신의 요점을 이해하고 이제는 효과가 있습니다! – pearmak