I have an activity with 2 fragments, one handles all data and other is only like a scoreboard.
Fragment1 asks for a new player name when one player is out, and fragment2 will use the name for the scoreboard. I'm using a static method in fragment1 getPlayerName() and using it in fragment2.
The app consists of a ListView + custom ArrayAdapter combo to populate the fragment2.
Here's the simplified code with required details, it doesn't show errors but doesn't work. I press out, enter the new name, but it doesn't show up in the Scoreboard.
Tried notifydatasetchanged() at various places but seems like that isn't the solution.
FieldFragment.java
public class FieldFragment extends Fragment {
static String playerNames[] = new String[11];
int i = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_field, container, false);
Button out = (Button)rootView.findViewById(R.id.out);
out.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displayDialog();
}
});
return rootView;
}
public void displayDialog(){
final Dialog nameDialog = new Dialog(getActivity());
nameDialog.setContentView(R.layout.dialog);
Button ok = (Button)nameDialog.findViewById(R.id.ok_button);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText nameET = (EditText)nameDialog.findViewById(R.id.name_edit_text);
playerNames[i] = nameET.getText().toString();
nameDialog.dismiss();
}
});
Button cancel = (Button)nameDialog.findViewById(R.id.cancel_button);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nameDialog.dismiss();
}
});
nameDialog.show();
}
public static String getPlayerName(int playerNumber) {
return playerNames[playerNumber];
}
}
And ScoreboardFragment:
public class ScoreboardFragment extends Fragment {
public ScoreboardFragment() {
// Required empty public constructor
}
PlayerAdapter playerAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_scoreboard, container, false);
int i=0;
ArrayList<Players> players = new ArrayList<>();
players.add(new Players(getPlayerName(i++),0,0));
players.add(new Players(getPlayerName(i++),0,0));
playerAdapter = new PlayerAdapter(getActivity(), players);
ListView playerList = (ListView)rootView.findViewById(R.id.list_of_players);
playerList.setAdapter(playerAdapter);
return rootView;
}
}
What's wrong with the code?
Let know if you need more files