2012-09-16 21 views
0

我現在正在研究4名玩家的記分卡,用戶在頁面底部輸入4名玩家的分數,當按下+按鈕時,剛輸入的分數將添加到上面的滾動視圖。android記分卡 - 共享首選項和顯示

槽號被定義爲輪數,使得當用戶輸入相應的EditText作爲1,12,34,56,78時,應用可以識別並添加slot = 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); 
    } 

回答

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(); 
+0

謝謝!我明白你的觀點,現在它可行! – pearmak