I have a task to make a simple restaurant menu app in Android. So, the home page consist of Food button and Drink button. If Food button clicked, the food menu page will appear. If Drink button clicked, the drink menu page will appear.
MainActivity.java:
int x = 1;
public int value()
{
x = 1;
return x;
}
public void clickFood(View view)
{
value();
Intent intent = new Intent(MainActivity.this, MenuList.class);
startActivity(intent);
}
public int value2()
{
x = 2;
return x;
}
public void clickDrink(View view)
{
value2();
Intent intent = new Intent(MainActivity.this, MenuList.class);
startActivity(intent);
}
MenuList.java:
mainListView = (ListView) findViewById(R.id.mainListView );
int y = 1;
if(main.x == y)
{
// List of food
fooddrink = new String[]{"Fried Chicken", "Fried Rice"};
}
else
{
// List of drink
fooddrink = new String[]{"Ice Tea", "Ice Coffee"};
}
ArrayList<String> listFoodDrink = new ArrayList<String>();
listFoodDrink.addAll( Arrays.asList(fooddrink) );
listAdapter = new ArrayAdapter<String>(this, R.layout.menu_list_row, listFoodDrink);
mainListView.setAdapter( listAdapter );
The problem, the output of ListView is always display food menu, despite I click Drink button. I find that this is because x value in MainActivity.java doesn't return the value, so the int x value always = 1.
How am I doing wrong?
This is your problem:
MainActivity main = new MainActivity();
You create a new instance of MainActivity (not the one you've "come from") where x = 1.
Make x in MainActivity, for example, static like
static int x = 1;
and use it in MenuList.java as follows:
if(MainActivity.x == y)
{ ...
But!
That is NOT how you should go in Android taking into consideration its component's lifecycle (more on that). Once MainActivity has been destroyed by the system, x, as being static, is always = 1, unless another instance of MainActivity has changed it.
So, you may use several options, one of which would be to store x value somewhere, e.g. in SharedPreferences. Another one would be to pass the value within Intent's extra as per #VVJ's answer.
You have two activities MainActivity and MenuListActivity.In MainActivity there are two buttons Food and Restaurant. You have handle click event for each as follows:
foodButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MenuList.class);
intent.putExtra("type",1);//pass 1 for food
startActivity(intent);
}
});
restaurantButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MenuList.class);
intent.putExtra("type",2);//pass 2 for Restaurant
startActivity(intent);
}
});
Now in your MenuList activity you have to take value of key "type" and based on type value pass appropriate data source to adapter.You can do something like this(in onCreate of MenuList activity):
int type=getActivity().getIntent().getExtras().getInt("type");
i(type==1)
{
// List of food
fooddrink = new String[]{"Fried Chicken", "Fried Rice"};
}
else
{
// List of drink
fooddrink = new String[]{"Ice Tea", "Ice Coffee"};
}
ArrayList<String> listFoodDrink = new ArrayList<String>();
listFoodDrink.addAll( Arrays.asList(fooddrink) );
listAdapter = new ArrayAdapter<String>(this, R.layout.menu_list_row, listFoodDrink);
mainListView.setAdapter( listAdapter );
Related
i declare int score = 100 in my 1st activity and use it to my 2nd activity, now the int score is declared as array in 2nd act. but its declared in local variable so how can i use it to my 3rd activity?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level2);
ans1 = findViewById(R.id.ans1);
btn1 = findViewById(R.id.btn1);
Intent i = getIntent();
final int[] score2 = {i.getIntExtra("fscore", 0)};
scr1 = (TextView) findViewById(R.id.scr1);
scr1.setText(String.valueOf(score2[0]));
btn1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
String answer = ans1.getText().toString();
if (score2[0] == 0)
{
Toast.makeText(getApplication(),"You Lose!", Toast.LENGTH_SHORT).show();
gameover();
}
else
{
if (answer.equalsIgnoreCase("ANSWER2"))
{
Toast.makeText(getApplication(),"CORRECT!!", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplication(),"Wrong Answer -10 Points", Toast.LENGTH_SHORT).show();
score2[0] = score2[0] - 10;
scr1.setText(String.valueOf(score2[0]));
}
}
}
});
}
public void gameover()
{
Intent intent = new Intent(this, gameover.class);
startActivity(intent);
}
}
If you want to pass score on game over then your funtion body should look like :
Intent intent = new Intent(this, gameover.class).putExtra("score",yourIntValue);
startActivity(intent);
In your second activity :
intent.getIntExtra("score", defValue); // store in a variable or add in an array
it will give you that int value you passed and you can either add it in your array or pass it to third activity using same method as above.
One thing I noticed that you got an int array because you tried making that variable final to use in inner functions right?
final int[] score2 = {i.getIntExtra("fscore", 0)};
Why don't you go for declaring a global scope variable with private access if you are using the same value multiple times.
private int score2;
assign it a value where you have written final int[ ] score2 = {i.getIntExtra("fscore", 0)};,
score2 = i.getIntExtra("fscore",0);
now use score2 wherever you want.There won't be a need of taking array here
I'm having problem in storing the intent.putExtra inside the android local database. I am creating a game like 4Pics1Word for my project. It has only 25 levels so I created 25 activities and I randomized it. After solving a particular activity, it will then be removed to the ArrayList of Classes, ArrayList<Class>. Now, I used intent.putExtra("ACTIVITY_LIST", activityList); to store the intent and to pass it to the next activity. My problem is I can't store it on local database. When I exit the game the progress is not saved, it starts again from the first level. Any suggestions? Thank you!
Here's my code in my Main Activity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button btnStart;
Context context;
SharedPreferences mPrefs = getPreferences(MODE_PRIVATE);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnStart = (Button) findViewById(R.id.btnStart);
btnStart.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// We are creating a list, which will store the activities that haven't been opened yet
ArrayList<Class> activityList = new ArrayList<>();
activityList.add(first.class);
activityList.add(second.class);
activityList.add(third.class);
activityList.add(fourth.class);
activityList.add(fifth.class);
Random generator = new Random();
int number = generator.nextInt(5) + 1;
Class activity = null;
switch(number) {
case 1:
activity = first.class;
activityList.remove(first.class);
break;
case 2:
activity = second.class;
activityList.remove(second.class);
break;
case 3:
activity = third.class;
activityList.remove(third.class);
break;
case 4:
activity = fourth.class;
activityList.remove(fourth.class);
break;
default:
activity = fifth.class;
activityList.remove(fifth.class);
break;
}
Intent intent = new Intent(getBaseContext(), activity);
intent.putExtra("ACTIVITY_LIST", activityList);
SharedPreferences.Editor prefsEditor = mPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(activityList); // myObject - instance of MyObject
prefsEditor.putString("MyObject", json);
prefsEditor.commit();
startActivity(intent);
}
}
Here's my code in my first activity:
public class first extends AppCompatActivity implements View.OnClickListener{
EditText etAnswer;
Button btnGo;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
etAnswer = (EditText) findViewById(R.id.etAnswer);
btnGo = (Button) findViewById(R.id.btnGo);
btnGo.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btnGo:
String answer = etAnswer.getText().toString();
if(answer.equals("Jose Rizal") || answer.equals("jose rizal") || answer.equals("Rizal") || answer.equals("rizal") ){
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("The famous Rizal monument in Luneta was not the work of a Filipino but a Swiss sculptor named Richard Kissling?" +
"\n" +
"\n" +
"Source: http://www.joserizal.ph/ta01.html");
dlgAlert.setTitle("Did you know that ...");
dlgAlert.setPositiveButton("Next",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ArrayList<Class> activityList = new ArrayList<>();
Bundle extras = getIntent().getExtras();
activityList = (ArrayList<Class>) extras.get("ACTIVITY_LIST");
if(activityList.size() == 0) {
Context context = getApplicationContext();
CharSequence last = "Congratulations! You just finished the game! Please wait for the next update!";
int durationFinal = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, last, durationFinal);
toast.show();
} else {
// Now, the random number is generated between 1 and however many
// activities we have remaining
Random generator = new Random();
int number = generator.nextInt(activityList.size()) + 1;
Class activity = null;
// Here, we are checking to see what the output of the random was
switch(number) {
case 1:
// We will open the first remaining activity of the list
activity = activityList.get(0);
// We will now remove that activity from the list
activityList.remove(0);
break;
case 2:
// We will open the second remaining activity of the list
activity = activityList.get(1);
activityList.remove(1);
break;
case 3:
// We will open the third remaining activity of the list
activity = activityList.get(2);
activityList.remove(2);
break;
case 4:
// We will open the fourth remaining activity of the list
activity = activityList.get(3);
activityList.remove(3);
break;
default:
// We will open the fifth remaining activity of the list
activity = activityList.get(4);
activityList.remove(4);
break;
}
// Note: in the above, we might not have 3 remaining activities, for example,
// but it doesn't matter because that case wouldn't be called anyway,
// as we have already decided that the number would be between 1 and the number of
// activities left.
// Starting the activity, and passing on the remaining number of activities
// to the next one that is opened
Intent intent = new Intent(getBaseContext(), activity);
intent.putExtra("ACTIVITY_LIST", activityList);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}
});
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}else{
Context context = getApplicationContext();
CharSequence text = "Wrong! Try Again.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
break;
}
}
}
Everytime you come to MainActivity , you new an ArrayList and add all the activities rather than get the cache from your local SharedPreferences .
When you finish one game in an Activity , you did not save your progress in cache.
After updating the arrayList,save your arrayList like this :
SharedPreferences.Editor prefsEditor = mPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(activityList); // myObject - instance of MyObject
prefsEditor.putString("MyObject", json);
prefsEditor.commit();
And when you want to read the arrayList saved ,do like this:
String arrayStr = mPrefs.getString("myObject","defValue");
Gson gson = new Gson();
List<Class> array = gson.fromGson(arrayStr,new TypeToken<List<Class>>(){}.getType());
if(array==null){
array = new ArrayList<>();
array.add(...);
}
I have a listView in Activity A , which the value are returned from Activity B.When the list is clicked, it will intent to Activity B for edit.
Activity B
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.add_details_information);
addItemsOnSpinner();
if(getIntent().getExtras()!=null) // if has value pass from A
{
final String Project1=getIntent().getStringExtra("ReceiveProject");
final String Description1=getIntent().getStringExtra("ReceiveDescription");
final String Progress1=getIntent().getStringExtra("ReceiveProgress");
final String TimeIn1=getIntent().getStringExtra("ReceiveTimeIn");
final String TimeOut1=getIntent().getStringExtra("ReceiveTimeOut");
//project.setText(Project1);
description.setText(Description1);
//progressText.setText("Covered:")
timeIn.setText(TimeIn1);
timeOut.setText(TimeOut1);
}
save.setOnClickListener(new View.OnClickListener()
{ // return to A
#Override
public void onClick(View v)
{
Intent returnIntent=new Intent();
Project=project.getSelectedItem().toString(); // Spinner Value
Description=description.getText().toString(); //from editText
progress=seekBar.getProgress(); // From SeekBar
returnIntent.putExtra("Project",Project);
returnIntent.putExtra("Description", Description);
returnIntent.putExtra("progress", progress);
Toast.makeText(getApplicationContext(), progress+"", Toast.LENGTH_LONG).show();
returnIntent.putExtra("TimeIn", TimeIn);
returnIntent.putExtra("TimeOut",TimeOut);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
});
public void addItemsOnSpinner()
{
project=(Spinner)findViewById(R.id.SpinnerProject);
List<String> list = new ArrayList<String>();
list.add("TRN-XXX-XXX");
list.add("Pro-XXX-XXX);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.spinner_item, list);
//adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
project.setAdapter(adapter);
}
Activity A
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { // if listView is clicked
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
mClickedPosition = position;
Intent i = new Intent(getApplication(), Add_Details_Information.class);
i.putExtra("ReceiveProject", ReceiveProject);
i.putExtra("ReceiveDescription", ReceiveDescription);
i.putExtra("ReceiveProgress", ReceiveProgress);
i.putExtra("ReceiveTimeIn", ReceiveTimeIn);
i.putExtra("ReceiveTimeOut", ReceiveTimeOut);
startActivityForResult(i,PROJECT_REQUEST_CODE);
}
});
}
I know that we can use setText to display the passed value from A to B for editText, but how about the spinner and seekBar value ?
This is the listView in Activity A. Value are returned from Activity B.
When listView is clicked, it will goes to B again to edit.
So how can I make the spinner in B display Pro-XXX-XXX and the seekBar goes to 48 ? Any idea or suggestion ? Thanks a lot
Edited
After used the answer from #Clairvoyant, now I get this (for spinner value).
Activity A
There are 4 list in Activity A.
Assume first list is clicked.
Everything works fine just the spinner(Project/Service/Training) display wrong value. It display the spinner value from last list(PRO-SKM-D5) instead of itself(Pro-XXX-XXX)
First Step: Make your addItemsOnSpinner like as below:
public void addItemsOnSpinner(String value)
{
int position = 0;
project=(Spinner)findViewById(R.id.SpinnerProject);
List<String> list = new ArrayList<String>();
list.add(position,"TRN-XXX-XXX");
list.add("Pro-XXX-XXX");
for(int i=0; i<list.size() ; i++){
if(list.get(i).equalsIgnoreCase(value)){
position = i;
break;
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String> (getApplicationContext(),R.layout.spinner_item, list);
//adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
project.setAdapter(adapter);
project.setSelection(position);
}
Second Step: call the above method when you are assigning value to the variable you have to show in spinner here for eg: project1 is the string value which you want to show in spinner then call the method as follows:
final String Project1=getIntent().getStringExtra("ReceiveProject");
addItemsOnSpinner(Project1);
Use SharedPreferences. Next time, googling helps.
Get SharedPreferences
SharedPreferences prefs = getDefaultSharedPreferences(context);
Read preferences:
String key = "test1_string_pref";
String default = "returned_if_not_defined";
String test1 = prefs.getString(key, default);
To edit and save preferences
SharedPreferences.Edtior editor = prefs.edit(); //Get SharedPref Editor
editor.putString(key, "My String");
editor.commit();
Shorter way to write
prefs.edit().putString(key, "Value").commit();
Additional info for SharedPreferences: JavaDoc and Android Developers Article
I am developing a quiz application with two navigation buttons (back and next), and each quiz has an array of 30 questions. Now when btnnext reaches the last question, it should open another activity(Review) when u click on it again
next = (Button)findViewById(R.id.next);
next.setVisibility(View.GONE);
next.setOnClickListener(nextListener);
private View.OnClickListener nextListener = new View.OnClickListener() {
public void onClick(View v) {
setAnswer();
quesIndex++;
if (quesIndex >= QuizFunActivity.getQuesList().length())
quesIndex = QuizFunActivity.getQuesList().length() - 1;
showQuestion(quesIndex,review);
}
};
next = (Button)findViewById(R.id.next);
next.setVisibility(View.GONE);
next.setOnClickListener(nextListener);
private View.OnClickListener nextListener = new View.OnClickListener() {
public void onClick(View v) {
setAnswer();
if (quesIndex != QuizFunActivity.getQuesList().length()-1) {
quesIndex++;
showQuestion(quesIndex,review);
}
else {
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
myIntent.putExtra("key", value); //Optional parameters
CurrentActivity.this.startActivity(myIntent);
}
if (quesIndex == QuizFunActivity.getQuesList().length()-1)
next.setText("Finish");
}
};
Add the piece of code for next activity
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
myIntent.putExtra("key", value); //Optional parameters
CurrentActivity.this.startActivity(myIntent);
You have to check for the last question and change the button view text to finish.
To all you that have helped me with my other questions thank you. I almost have it, but 2 final problems are preventing it from working the way i want.
These 2 classes are supposed to do as follows. 1st class gets the names of the people that want to play the game. Uses the same EditText and when they input their name they click submit. When all the names are submitted they click the done/play button which sends them and their data (how many players and names) to the next class. On class 1 i believe the error lies in the submit button. I'm trying to add all the names to an array list and I dont believe it is doing it correctly. When I run the app it takes in the names just fine from the users standpoint. But on the following screen it should display their name: (it says null so it is not getting the names correctly) and a task to do (which it does correctly).
The last thing it needs to do is on class 2 it needs to allow those buttons (failed, champ, and not bad) to only need to be clicked once (then it sets a score to the name of the person who's turn it was) and then it needs to start the next person and task. (It does neither atm). I would really appreciate help getting this blasted thing to work. Thanks to all who take the time to reply. And sorry if ur sick of seeing my help requests.
Class 1
public class Class1 extends Activity
{
int players=0, i=0;
String names[];
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.class1);
final EditText input = (EditText) findViewById(R.id.nameinput);
final ArrayList<String> names = new ArrayList<String>();
//names = new String[players];
Button submitButton = (Button) findViewById(R.id.submit_btn);
submitButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View submit1)
{
//for( i=i; i < players; i++)
//{
players++;
names.add(input.getText().toString());
//names[i] = input.getText().toString();
input.setText("");
//}
}
});
Button doneButton = (Button) findViewById(R.id.done_btn);
doneButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View done1)
{
Intent done = new Intent(Class1.this, Game.class);
Bundle bundle = new Bundle();
bundle.putStringArrayList("arrayKey", names);
done.putExtra("players", players);
//done.putExtra("names", names[players]);
startActivity(done);
}
});
}
Game Class
public class Game extends Activity
{
int players, counter=0, score, ptasks,rindex;
String[] names;
String[] tasks;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
Bundle bundle = this.getIntent().getExtras();
String[] names = bundle.getStringArray("arrayKey");
Intent game = getIntent();
players = game.getIntExtra("players", 1);
//names = game.getStringArrayExtra("names");
Random generator = new Random();
tasks = new String[10];
tasks[0]= "";
tasks[1]= "";
tasks[2]= "";
tasks[3]= "";
tasks[4]= "";
tasks[5]= "";
tasks[6]= "";
tasks[7]= "";
tasks[8]= "";
tasks[9]= "";
names = new String[players];
while (counter <5)
{
for (int i = 0; i < players; i++)
{
TextView name1 = (TextView) findViewById(R.id.pname);
name1.setText( names[i]+":");
ptasks = 10;
rindex = generator.nextInt(ptasks);
TextView task = (TextView) findViewById(R.id.task);
task.setText( tasks[rindex]);
Button failButton = (Button) findViewById(R.id.fail_btn);
failButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View failed)
{
return;
}
});
Button notButton = (Button) findViewById(R.id.notbad_btn);
notButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View notbad)
{
return;
}
});
Button champButton = (Button) findViewById(R.id.champ_btn);
champButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View champp)
{
return;
}
});
}
counter++;
}
}
}
As a side note. The things that you see within those sections that have // comments next to them I have there because i was testing out between those and the ones that arent commented out and neither worked. If you have any input on fixing any of this i appreciate it.
I see two problems with your code that might explain why you get a null for your players list in your second Activity:
In Game, String[] names = bundle.getStringArray("arrayKey"); should be
ArrayList<String> names = bundle.getStringArrayList("arrayKey");`
In Class1, you're putting the ArrayList into the Bundle(bundle.putStringArrayList("arrayKey", names);) which is pointless since bundle goes no where. You should be putting it into the Intent instead:
done.putStringListExtra("arrayKey", names);
Note that your code is all the more confusing because you have both a String [] named names and an ArrayList named names in different scopes. Decide on one (I'd recommend the List) and get rid of the other.
Also, in Game, this is unncessary:
Bundle bundle = this.getIntent().getExtras();
String[] names = bundle.getStringArray("arrayKey");
Intent game = getIntent();
players = game.getIntExtra("players", 1);
You already have the bundle just before this, so you could as well do:
Bundle bundle = this.getIntent().getExtras();
String[] names = bundle.getStringArray("arrayKey");
players = bundle.getInt("players", 1);
The basic concept is that from the calling activity, you put information into an Intent using the various putExtra() and putExtraXXX() methods. In the called activity, you get the information you had put into the Intent by either
getting a Bundle *from * the Intent via getExtras() and then getting everything put in using the various get() methods on the Bundle (not the Intent).
directly invoking the getExtraXXX() methods on the Intent.
For the second part, as your code currently stands, it simply going to loop over all the players immediately (5 times in all, I don't understand the purpose of counter).
What you should instead be doing is performing all of your processing (calculating the score for the current player, incrementing the value of the player index, setting the next task etc) only when one of the 3 buttons is pressed. If it's going to be a long-lived task, you could disable the buttons until finished in order to enforce the requirement of allowing only one button to be pressed per player. Re-enable the buttons when the next player is ready.
I don't have the energy to churn out everything you need but at a starting point, turn this:
public void onCreate(Bundle savedInstanceState)
{
//...other code here
while (counter <5)
{
for (int i = 0; i < players; i++)
{
TextView name1 = (TextView) findViewById(R.id.pname);
name1.setText( names[i]+":");
ptasks = 10;
rindex = generator.nextInt(ptasks);
TextView task = (TextView) findViewById(R.id.task);
task.setText( tasks[rindex]);
Button failButton = (Button) findViewById(R.id.fail_btn);
failButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View failed)
{
return;
}
});
Button notButton = (Button) findViewById(R.id.notbad_btn);
notButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View notbad)
{
return;
}
});
Button champButton = (Button) findViewById(R.id.champ_btn);
champButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View champp)
{
return;
}
});
}
counter++;
}
//...other code here
}
into
public void onCreate(Bundle savedInstanceState)
{
//...other code here
int i = 0;
TextView name1 = (TextView) findViewById(R.id.pname);
TextView task = (TextView) findViewById(R.id.task);
Button failButton = (Button) findViewById(R.id.fail_btn);
failButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View failed)
{
//do what must be done for the current player, calculate score, etc
prepareNextPlayer(++i, names, name1, task);
}
});
Button notButton = (Button) findViewById(R.id.notbad_btn);
notButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View notbad)
{
//do what must be done for the current player, calculate score, etc
prepareNextPlayer(++i, names, name1, task);
}
});
Button champButton = (Button) findViewById(R.id.champ_btn);
champButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View champp)
{
//do what must be done for the current player, calculate score, etc
prepareNextPlayer(++i, names, name1, task);
}
});
//...other code here
}
private void prepareNextPlayer(int i, ArrayList<String> names, String [] tasks, TextView nameField, TextView taskField)
{
if(i >= names.size())
{
//all players have been processed, what happens now?
return;
}
int rindex = generator.nextInt(10);
nameField.setText( names.get(i)+":");
task.setText( tasks[rindex]);
}