我正在制作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,但上面输入的所有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 :(得分: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();