Change textViews when they equal each other? - android

I have two textViews in my app right now. One textView serves as a Counter and the other servers as the Limit. What I want to accomplish is when the Counter equals the Limit, the Counter is set back to ZERO and the Limit is increased by 100.
private int i = 0;
Spinner spinnerMonsters = (Spinner) findViewById(R.id.spinnerMonsters);
TextView textViewBattleResults = (TextView) findViewById(R.id.textViewBattleResults);
TextView textViewXPValue = (TextView) findViewById(R.id.textViewXPValue);
TextView textViewXpNextLevel = (TextView) findViewById(R.id.textViewXpNextLevel);
if (textViewXPValue.getText().toString().equals(textViewXpNextLevel.getText().toString())) {
int newLimit = Integer.parseInt(textViewXpNextLevel.getText().toString()) + 100;
textViewXpNextLevel.setText(newLimit + "");
textViewXPValue.setText("0");
}
else {
textViewXPValue.setText(textViewXPValue.getText().toString());
textViewXpNextLevel.getText().toString();
}
//Monster sequences
if (spinnerMonsters.getSelectedItem().toString().equals("(0) Training Dummy")) {
textViewBattleResults.setText("You have killed Training Dummy for 10 experience points!");
i = i+10;
textViewXPValue.setText(String.valueOf(i));
}
else {
textViewBattleResults.setText(" ");
textViewXPValue.setText(textViewXPValue.getText().toString());
}
if (spinnerMonsters.getSelectedItem().toString().equals("(2) Cockroach")) {
textViewBattleResults.setText("You have killed a Cockroach for 27 experience points!");
i = i+27;
textViewXPValue.setText(String.valueOf(i));
}
else {
textViewBattleResults.setText(" ");
textViewXPValue.setText(textViewXPValue.getText().toString());
}
}
});
The problem here is that the counter will count up by ten when the button is clicked. But when the Counter equals the Limit, the Limit increases by 100 and the Counter remains at the same number and doesn't reset back to ZERO.
Any suggestions..?
EDIT
Also, how would i do the same thing but if the Counter was Equal to or Greater than the Limit?

You don't reset value of i to zero. You first set text of xpvalue to zero, but then you set it's text to current value ofi, which is not zero.

Related

how to add 1 decimal place to score system

Im trying to make the score of my inspection have 1 decimal place so i can have more precise answer.
result = (TextView) findViewById(R.id.displaym1score);
sum=0;
total=0;
box1 = (CheckBox) findViewById(R.id.box1);
box1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (box1.isChecked()) {
sum += 1; int total = 100*sum/37;
result.setText(total + "%");
}
else {
sum -= 1; int total = 100*sum/37;
result.setText(total + "%");
}
}
});
each box you check it updates instantly and the score just needs to have a decimal place like "72.1%" instead of "72%". there is also 36 more boxes set up the same way.
Ive tried a few things and i cannot figure it out. what do i do?
First total must be a float or a double, not int
instead setText( ... put:
result.setText(String.format("%.1f", total) + "%"); // 1 is the decimal places you want
Probably the easiest way is to use a double instead of an int then format the output to display one decimal place.
use double for total and use following method to get your answer.
public double getFormattedTotal(double total){
DecimalFormat precision = new DecimalFormat("0.0");
return precision.format(total);
}

Modify variable of textview

int triesno=5;
final TextView tries= (TextView) findViewById(R.id.tries);
tries.setText("you have "+triesno.toString()+" tries left");
I want to make a game that when the user sends wrong input he gets less tries. The above code prints the initial number, but I want to modify only the variable and not to update like:
triesno--;
tries.setText("you have "+triesno.toString()+" tries left");
How can I do that?
int tries = 5;
final TextView triesTv = (TextView) findViewById(R.id.tries);
tries = tries - 1;
String triesStr = String.format("you have: %d tries left", tries);
triesTv.setText(triesStr);
Are you sure about your question?
final TextView tvTries = (TextView) findViewById(R.id.tries);
int tries = Integer.parseInt(tvTries.getText()); // Get current number of time that you can try to.
tvTries .setText("you have "+ --tries + " tries left"); // -1 and set to textview

How to Double Score the point for a word?

I have my first Android app and I am having difficulty on how can I double the point for a word/words whenever a player click an imageview. Basically, I want it to function like this: If the player hasn't click the imageview, the pointing system stays the same. If the player clicks the imageview the a message will pop-out (a toast perhaps telling the player that the points for the next 3 words will be doubled). How can I make this happen? How can I double the points for the 3 consecutive words without exceeding?
Here's my code for searching and calculation
//CALCULATE SCORE
private int optionTxtView = 0 ; //which textview to use for addition purposes
private int addClick = 0 ; //No. of clicks for search for calculation
private void calculate(){
x = Integer.parseInt(tv3.getText().toString().replaceAll("\\s",""));
y = Integer.parseInt(tv2.getText().toString().replaceAll("\\s",""));
z = x + y;
score.setText(Integer.toString(z));
}
//SEARCH WORD, DISPLAYING SCORE
public void viewWord(View view)
{
String s1= search.getText().toString(); //What word to search
String s2= dbHelper.getData(s1); // If in db, display equivalent score
if(optionTxtView == 0){
//display the score on textview1
tv2.setText(s2);
optionTxtView = 1;
}
else{
if(optionTxtView == 1){
//display the score on textview2
tv3.setText(s2);
optionTxtView = 0;
}
}
//Display search word/s
adapter.add(text.getText().toString());
adapter.notifyDataSetChanged();
if(addClick ==0){
calculate();
addClick = 1;
text.setText("");
generatedString="";
}
else{
if(addClick == 1){
calculate();
addClick = 2;
text.setText("");
generatedString="";
}}
boolean sDouble = false
imgView.setOnClickListener(new View.OnClickListener() {
//#Override
public void onClick(View v) {
// Show your Toast message
// double = true;
}
});
and where you are running your game code check the value of sDouble, if its true then double the scoring system inside a for loop, or set an integer value to 0 and increment it for next 3 words, after that set the value of sDouble back to false.
You may also need to make sure that the user doesn't click on the image for the next 3 tries after clicking it once.
Update:
Since your score is a String, you need to parse it to an Integer before performing any mathematical operation, use Integer.parseInt(s2).

list.getchildAt(i) return null pointer ,only shows visible views not shows more

I have a list view, in that list view, I have 30 items with edit text in which I manually put the value and after that I calculate the total of value set on edit text for 30 items. But problems is it calculate the total of only visible list items which are almost 10 and I need all 30 values of edit text.after 10 items view become null.
How to resolve this problem.
totalpoints.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
/*for (int i = 0; i < summaryList.size(); i++)
{
summaryList.get();
}*/
for (int i = 0; i < listView_subCategory.getCount(); i++) {
view = listView_subCategory.getChildAt(i);
if (view == null) {
} else {
try {
EditText text = (EditText) view
.findViewById(R.id.et_date);
TextView tv_productname=(TextView) view.findViewById(R.id.textView_userName);
String points = text.getText().toString().trim();
String productname=tv_productname.getText().toString().trim();
int point = Integer.valueOf(points);
count_summary = count_summary + point;
String truetotal = Integer.toString(count_summary);
et_total.setText(truetotal);
} catch (Exception e) {
Toast toast1 = Toast.makeText(getActivity(),
"Fill Value First", Toast.LENGTH_SHORT);
toast1.setGravity(Gravity.CENTER, 0, 0);
toast1.show();
}
}
}
count = 0;
}
});
I guess you should think fo a better approach than iterating over the EditTexts with getChildAt() to collect all the value.
I take it that the user can change the values in the EditTexts, you can have an Array of size 30 of Values and everytime the user enters / changes a value you change the according value in the Array. Use a listener that changes the values in your Array like this.

Comparing two integers in android

I want to compare two sets of digits. The first I will generate as a random in code. The second will be entered into an EditText.
I wish to compare the two numbers and see if they match.
Random number:
public static int random() {
if (randomNumberGenerator == null)
initRNG();
return randomNumberGenerator.nextInt();
}
private static Random randomNumberGenerator;
private static synchronized void initRNG() {
if (randomNumberGenerator == null)
randomNumberGenerator = new Random();
}
Showing randomly selected number
display = (TextView) findViewById (R.id.textView1);
display.setText ("Random Number:" + random ());
How can I compare the two numbers?
You can use something like the following:
display = (TextView) findViewById (R.id.textView1);
EditText myEditText = ...; // Find your EditText, maybe by ID?
int random = random(); // Get your random value
display.setText("Random Number:" + random()); // Show the random number
int numberEntered = -1; // Default value for numberEntered; should be a value you wouldn't get randomly
try {
// Here, we try to make a number out of the EditText; if it fails, we get a Toast error
numberEntered = Integer.parseInt(myEditText.getText().toString());
} catch (NumberFormatException nfe) {
Toast.makeText(myEditText.getContext(), "That's not a number!", Toast.LENGTH_LONG).show(); // Tell the user they didn't enter a number
}
if (random == numberEntered) {
// HAPPY DAY, THEY MATCH!
} else {
// No luck :(
}
Just make a subtraction between both numbers and if the result is 0, both numbers match.
EDIT: Subtraction was just a way to show that both were numbers. Obviously it makes more sense to compare both numbers directly rather than subtracting and comparing then.

Categories

Resources