Question: This is android studio. Each time the user clicks the button winButtonOne, in the GameEmulator class, I want the variable score, in my Scoreboard class,
This is android studio. Each time the user clicks the button winButtonOne, in the GameEmulator class, I want the variable score, in my Scoreboard class, to increase by one so that my Listview, selectview3 in Scoreboard class, updates the variable score and increases it by 1. I do not know any other ways to try and do this. Here is what I have so far:
public class GameEmulator extends Activity{ //Creating two static values to pass strings from SelectPlayer classes public final static String value = "EMPTY_VALUE"; public final static String value2 = "EMPTY_VALUE2"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout); //Button created to go back to AddPlayer activity Button addplayer1 = findViewById(R.id.button9); addplayer1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(GameEmulator.this, AddPlayer.class); startActivity(i); } }); Button viewScores = findViewById(R.id.viewScore); viewScores.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(GameEmulator.this, MainActivity.class); startActivity(intent); } }); //Button for player one winning Button winButtonOne = findViewById(R.id.button7); winButtonOne.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(GameEmulator.this, Scoreboard.class); intent.putExtra("VARIABLE", 1); startActivity(intent); } }); TextView textView = findViewById(R.id.name1); TextView textview2 = findViewById(R.id.name2); //setting value retrieved from SleectPlayer and Displaying it in textView Intent intent = getIntent(); String extra = intent.getStringExtra(value); textView.setText(extra); //setting value retrieved from SleectPlayer2 and Displaying it in textView2 Intent in = getIntent(); String extra1 = in.getStringExtra(value2); textview2.setText(extra1); } } public class Scoreboard extends Activity{ public static ArrayAdapter adapter2; public static ArrayAdapter adapter3; public static ArrayList list2 = new ArrayList<>(); public static ArrayList list3 = new ArrayList<>(); ListView selectView3; ListView selectView4; public static int losses1 = 0; public static int ties1 = 0; public static int losses2 = 0; public static int ties2 = 0; public final static String value2 = "EMPTY_VALUE2"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.scoreboard); selectView3 = findViewById(R.id.selectview3); selectView3.setVisibility(View.VISIBLE); selectView4 = findViewById(R.id.selectview4); selectView4.setVisibility(View.VISIBLE); //Using adapter for ListView menu adapter2 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list2); selectView3.setAdapter(adapter2); //Using intent to retrieve string from AddPlayer Activity Intent i = getIntent(); int score = i.getIntExtra("VARIABLE", 0); String data = i.getStringExtra("text_key"); if(data != null){ list2.add("Player 1"+" "+"Name: "+data+" "+"Wins: "+ score +" "+"Losses: "+ losses1+" "+"Ties: "+ ties1); } if(data != ""){ changeList(); } adapter3 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list3); selectView4.setAdapter(adapter3); Intent intent = getIntent(); String extra= intent.getStringExtra(value2); if(extra != null) { list3.add("Player 2" + " " + "Name: " + extra + " " + "Wins: " + score + " " + "Losses: " + losses2 + " " + "Ties: " + ties2); } if(data != ""){ changeList(); } } public void changeList() { adapter2.notifyDataSetChanged(); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
