Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This is android studio. I can't figure out why I am unable to ope SelectPLayer.java and SelectPLayer2.java from my MainActivity.java class. It has something to
This is android studio. I can't figure out why I am unable to ope SelectPLayer.java and SelectPLayer2.java from my MainActivity.java class. It has something to do with the way I am using intent get.extra from my AddPLayer.java class I beleieve. I am able to populate the listview using Edittext but then the buttons will not open my activities. Please help.
//MainActivity UI to create buttons for navigating to 5 menus public class MainActivity extends AppCompatActivity { //Declare variables Button gameStart; Button addPlayer; Button selectPlayer1; Button selectPlayer2; //Create OnCLickListener for buttons View.OnClickListener listener1 = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Use listener with switch case scenarios to navigate to different UIs listener1 = new OnClickListener() { @Override public void onClick(View v) { switch(v.getId()){ case R.id.startgame: startActivity(new Intent(MainActivity.this,GameEmulator.class)); break; case R.id.addplayerbutton: startActivity(new Intent(MainActivity.this,AddPlayer.class)); break; case R.id.selectplayer1: startActivity(new Intent(MainActivity.this,SelectPlayer.class)); break; case R.id.selectplayer2: startActivity(new Intent(MainActivity.this,SelectPlayer2.class)); } } }; //Setting content to id buttons and set listeners setContentView(R.layout.activity_main); gameStart= findViewById(R.id.startgame); gameStart.setOnClickListener(listener1); addPlayer= findViewById(R.id.addplayerbutton); addPlayer.setOnClickListener(listener1); selectPlayer1= findViewById(R.id.selectplayer1); selectPlayer1.setOnClickListener(listener1); selectPlayer2= findViewById(R.id.selectplayer2); selectPlayer2.setOnClickListener(listener1); } }
//Addplayer class to add players using intent to separate activities public class AddPlayer extends Activity{ //Declare buttons and edittext Button listButtonplayer1; Button listButtonplayer2; EditText playerIn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.playerinput); //Identify buttons and editext playerIn = (EditText) findViewById(R.id.inputname); listButtonplayer1 = (Button)findViewById(R.id.addbutton); listButtonplayer1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Using intent to send input to SelectPLayer Class Intent i = new Intent(AddPlayer.this,SelectPlayer.class); //Using string text_key to get and send string through intent i.putExtra("text_key", playerIn.getText().toString()); //Start intent activity startActivity(i); } }); //Second button I am trying to use for SelectPLayer2 activity only. listButtonplayer2 = (Button)findViewById(R.id.addbutton2); listButtonplayer2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Using intent to send input to SelectPLayer2 Class Intent in = new Intent(AddPlayer.this, SelectPlayer2.class); in.putExtra("text_key", playerIn.getText().toString()); startActivity(in); } }); } }
//Class to select player 1 public class SelectPlayer extends Activity { //Public static array list with adapter to crete the array and reference for listview being sent by intent from AddPLayer class public static ArrayListlist = new ArrayList<>(); public static ArrayAdapter adapter; public static ListView selectView; TextView title; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.selectplayer); //Identify selectview ListView selectView = findViewById(R.id.selectview); selectView.setClickable(true); selectView.setVisibility(View.VISIBLE); //Using adapter for ListView menu adapter = new ArrayAdapter (this, android.R.layout.simple_list_item_1, list); selectView.setAdapter(adapter); selectView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { String item = (String) parent.getItemAtPosition(position); // finding the item which has been clicked by the user Intent intent = new Intent(SelectPlayer.this, GameEmulator.class); //starting an intent to call GameEmulator Activity intent.putExtra(GameEmulator.value, item);// Putting the value clicked by user in intent startActivity(intent); // starting GameEmulator Activity } }); //Using intent to retrieve string from AddPlayer Activity Intent i = getIntent(); String data = i.getStringExtra("text_key"); list.add(data); changeList(); } public void changeList() { adapter.notifyDataSetChanged(); } }
//Class to select player 2 public class SelectPlayer2 extends Activity { //Public static array list with adapter to crete the array and reference for listview being sent by intent from AddPLayer class public static ArrayListlist1 = new ArrayList<>(); public static ArrayAdapter adapter1; Intent in; ListView selectView2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.selectplayer2); selectView2 = (ListView) findViewById(R.id.selectview1); selectView2.setClickable(true); selectView2.setVisibility(View.VISIBLE); //Using adapter for ListView menu adapter1 = new ArrayAdapter (this,android.R.layout.simple_list_item_1,list1); selectView2.setAdapter(adapter1); selectView2.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { String item =(String) parent.getItemAtPosition(position); // finding the item which has been clicked by the user Intent in = new Intent(SelectPlayer2.this, GameEmulator.class); //starting an intent to call GameEmulator Activity in.putExtra(GameEmulator.value, item);// Putting the value clicked by user in intent startActivity(in); // starting GameEmulator Activity } }); //Using intent to retrieve string from AddPlayer Activity in = getIntent(); String data = in.getStringExtra("text_key"); list1.add(data); changeList(); } public static void changeList() { adapter1.notifyDataSetChanged(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started