0

現在、ユーザーがページの下部に4人のプレーヤーのスコアを入力し、+ボタンを押すと、入力したスコアが上のスクロールビューに追加される4人のプレーヤーのスコアカードに取り組んでいます。

スロット番号はラウンド番号として定義され、ユーザーが対応するEditTextに1、12、34、56、78と入力すると、アプリはスロット= 1の行を認識して追加できるため、プレーヤー1のスコアは12になります。プレーヤー2は34、P3 = 56、P4=78になります。

次に、ユーザーが2、23、45、67、89などの別のセットを入力すると、ラウンド2などとして追加されます。これまでのところ、行を適切に追加でき、スロット番号は1、2、3などになりますが、理由はわかりません。

ラウンド1の場合:スロット番号は1として正しいが、上記の入力としての4人のプレーヤーのスコアはすべて78になります。

ラウンド2の場合:スロット番号は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);
   }
4

1 に答える 1

0

すべてのプレーヤーのスコアに同じSharedPrefsとキーを使用しているため、連続する書き込みごとにオーバーライドされます。

各プレーヤーにロードされる設定を変更するか、キーを一意にします。私は後者を行います。つまり、各プレーヤーのストアの概念を捨てて、次のようなことを行うこともできます。

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();
于 2012-09-16T19:10:43.993 に答える