0

我现在正在制作 4 名球员的记分卡,用户在页面底部输入 4 名球员的分数,当按下 + 按钮时,刚刚输入的分数将添加到上面的滚动视图中。

slot number 定义为整数,这样当用户在对应的 EditText 中输入 1、12、34、56、78 时,app 可以识别并添加一行 slot =1,则 Player 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 和 key,因此每次连续写入都会覆盖它。

更改为每个播放器加载的首选项,或使键唯一。我会做后者,这意味着你也可以为每个玩家放弃商店的概念,并做这样的事情:

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 回答