有人可以帮我解决下面的代码吗?
我遇到的问题是,在用户将数据输入警报对话框之前,行selectedPlayer.setScore(mScore);
会运行。
有没有办法修改我的代码,以便在用户将数据输入警报对话框后更新分数?
我觉得arrayPlayers
被捕获了,因为它是在片段的onCreateView中声明和填充的。有办法解决这个问题吗?
public class ScoringActivity extends AppCompatActivity {
DialogFragment mDialog;
static int mScore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scoring);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
public static class PlaceholderFragment extends Fragment {
ArrayList<Player> arrayPlayers;
ListView listViewPlayers;
PlayerAdapter adapter;
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
arrayPlayers = new ArrayList<Player>();
Intent intent = getActivity().getIntent();
int players = intent.getIntExtra("intPlayers", 0);
for (int i = 0; i<players; i++){
arrayPlayers.add(new Player("player " + i, 0));
}
adapter = new PlayerAdapter(getActivity(), arrayPlayers);
View rootView = inflater.inflate(R.layout.fragment_activity_scoring, container, false);
listViewPlayers = (ListView) rootView.findViewById(R.id.listview_playerscore);
listViewPlayers.setAdapter(adapter);
listViewPlayers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Player selectedPlayer = arrayPlayers.get(position);
String playerName = selectedPlayer.getName();
((ScoringActivity)getActivity()).showDialogFragment(playerName);
selectedPlayer.setScore(mScore);
}
});
return rootView;
}
public void refresh() {
adapter.notifyDataSetChanged();
}
}
void showDialogFragment(String playerName) {
mDialog = ScoreAlertDialogFragment.newInstance(playerName);
mDialog.show(getFragmentManager(),"SCORE DIALOG");
}
// Class that creates the AlertDialog
public static class ScoreAlertDialogFragment extends DialogFragment {
public static ScoreAlertDialogFragment newInstance(String playerName) {
ScoreAlertDialogFragment alertFragment = new ScoreAlertDialogFragment();
Bundle bundle = new Bundle();
bundle.putString("PlayerName", playerName);
alertFragment.setArguments(bundle);
return alertFragment;
}
// Build AlertDialog using AlertDialog.Builder
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.alertdialog_score, null);
final EditText editTextScore = (EditText) view.findViewById(R.id.edittext_score);
return new AlertDialog.Builder(getActivity())
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
.setView(view)
.setMessage("Enter " + getArguments().getString("PlayerName") + "'s Score:")
//Set up Yes Button
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, int id) {
mScore = Integer.parseInt(editTextScore.getText().toString().trim());
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
ScoreAlertDialogFragment.this.getDialog().cancel();
}
})
.create();
}
}
@Override
public void onResume() {
super.onResume();
PlaceholderFragment placeholderFragment = (PlaceholderFragment) getSupportFragmentManager().findFragmentById(R.id.container); //getFragmentManager().findFragmentById(R.id.p);
placeholderFragment.refresh();
}
}
谢谢!
答案 0 :(得分:0)
您是否尝试将selectedPlayer.setScore(mScore);
放入Dialog的onclicklistener?如果需要,您可以将selectedPlayer作为参数发送到Dialog。
答案 1 :(得分:0)
((ScoringActivity)getActivity()).showDialogFragment(selectedPlayer,view);
更改内部分数并使用视图设置分数 即 您将从视图中获取父级 view.getParent(); 尝试从他们的文本中设置文本。
答案 2 :(得分:0)
您应该在selectedPlayer
中传递.showDialogFragment()
作为参数,而不是可以在方法中检索的playerName。然后在player
中放置一个全局变量ScoreAlertDialogFragment
,并将player.setScore()
放入onClick
。
listViewPlayers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Player selectedPlayer = arrayPlayers.get(position);
((ScoringActivity)getActivity()).showDialogFragment(selectedPlayer);
}
});
void showDialogFragment(Player player) {
mDialog = new ScoreAlertDialogFragment(player);
mDialog.show(getFragmentManager(),"SCORE DIALOG");
}
public static class ScoreAlertDialogFragment extends DialogFragment {
Player player;
public ScoreAlertDialogFragment(Player player) {
..
this.player = player;
}
..
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, int id) {
score = Integer.parseInt(editTextScore.getText().toString().trim());
player.setScore(score);
}
})