Question
This is an assignment I am having trouble with. I am passing information from my EditText widgets into ListViews in seperate activities. I have my
This is an assignment I am having trouble with. I am passing information from my EditText widgets into ListViews in seperate activities. I have my 5 buttons in MainActivity but the classes I have listviews in will not open using the button from MainActivity. I can only access certain classes such as SelectPLayer and SelectPLayer2 using intent. I am having a lot of problems with this. Please let me know what I am doing wrong.
This app maintains the 2 players for a 2-player game. Each player must specify a name.
The Main Menu activity provides 5 buttons for starting the 5 activities of the app.
The Game Emulator activity displays player 1 and 2 and provides 3 buttons that can be used
to specify who won, lost, or tied the game.
The Scoreboard activity displays all players that are stored in the database as well as their
wins, losses, and ties.
The Select Player activity allows the user to select player 1 or 2 from a list of players thats
stored in the database.
The Add Player activity allows the user to add a new player to the database.
This is what I have so far:
//MainActivity UI to create buttons for navigating to 5 menus public class MainActivity extends AppCompatActivity implements View.OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Setting conent view for UI and 5 buttons setContentView(R.layout.activity_main); Button gameStart = (Button) findViewById(R.id.startgame); gameStart.setOnClickListener(this); Button addPlayer= (Button) findViewById(R.id.addplayerbutton); addPlayer.setOnClickListener(this); Button selectPlayer1= (Button) findViewById(R.id.selectplayer1); selectPlayer1.setOnClickListener(this); Button selectPlayer2= (Button) findViewById(R.id.selectplayer2); selectPlayer2.setOnClickListener(this); Button Scoreboard = (Button)findViewById(R.id.viewscoreboard); Scoreboard.setOnClickListener(this); } @Override public void onClick(View v) { //Using switch case to send user to new activities using intent calling method 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)); break; default: break; case R.id.viewscoreboard: startActivity(new Intent(MainActivity.this,Scoreboard.class)); break; } } }
//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 intent1 = new Intent(AddPlayer.this, SelectPlayer2.class); intent1.putExtra("text_key", playerIn.getText().toString()); startActivity(intent1); } }); } }
//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; //Using intent to retrieve string from AddPlayer Activity @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); Intent intent1 = new Intent(SelectPlayer.this, Scoreboard.class);// starting GameEmulator Activity } }); changeList(); } public void changeList() { //Using intent to retrieve string from AddPlayer Activity Intent i = getIntent(); String data = i.getStringExtra("text_key"); list.add(data); 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; public static 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 item2 =(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.value2, item2);// Putting the value clicked by user in intent startActivity(in); // starting GameEmulator Activity } }); changeList(); } public void changeList() { Intent intent1 = getIntent(); String data = intent1.getStringExtra("text_key"); list1.add(data); adapter1.notifyDataSetChanged(); } }
//Scoreboard class should store the players in a listview
public class Scoreboard extends Activity{ public static ArrayAdapteradapter4; public static ArrayList list2 = new ArrayList (); ListView selectView3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.scoreboard); selectView3 = (ListView) findViewById(R.id.selectview3); selectView3.setVisibility(View.VISIBLE); adapter4 = new ArrayAdapter (this,android.R.layout.simple_list_item_1,list2); selectView3.setAdapter(adapter4); changeList(); } public void changeList() { Intent i = getIntent(); String data = i.getStringExtra("text_key"); list2.add(data); adapter4.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