I am trying to set the score in a game but it isn't doing anything at the moment.
I have declared a score variable:
static int score = 0;
When an answer is shown in another textview as "Correct" I want the score to increment and show this in another textfield which will be for the score.
So far I have tried this:
public void score(){
check.getText();
if(check.equals("Correct")){
score++;
Score.setText(String.valueOf(score));
}
check is a Textfield which shows Correct or Incorrect. Score is another textfield.
and then I put the method score() into an onclick, but this doesn't update the textfield.
I would appreciate any advice on this.
Thanks
Edit:
check.equals("Correct")
should be
check.getText().equals("Correct")
You don't heed the check.getText() method call that you have by itself. And you need to use this for your if statement
if(check.getText().toString().equals("correct))
getText returns the object type Editable which is not a String. If you are getting a force close please edit you question to include the stack trace from the LogCat output.
Related
bted.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String textoDolars=dolars.getText().toString();
Double dolars=Double.parseDouble(textoDolars);
Double euros=dolars*1.19;
String textoEuros=String.valueOf(euros);
euros.setText(textoEuros);
}
});
In this part euros.setText(textoEuros);
can not resolve method 'setText(java.lang.String)'
How can ı handle this situation?
I tried another type of setText but ı couldnt find truth.Can you help me?
Since you haven't provided us with full code I cannot determine the correct answer but here are couple of possibilities.
Set your command to the following textview.setText("sometext")
You have to use "" when you are going to declare strings.
Dolars is not a textview
Is TextODolars a variable? If so, what kind? Maybe you have to use .toString to convert it
But please get into a habit of posting code for a clear answer
You are calling set text to a Double variable.
From your code: euros.setText(textoEuros);
Doubleis a wrapper class for the primitive type double and it does not have a method called setText.
You have a confusion there you probably are looking for a TextView on any subclass of it.
I think you wan to display the value on the screen(?).
First you need a TextView.
<TextView
android:id="#+id/output"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Now you can access it with some code to display the wanted value on the screen.
TextView output = (TextView)findViewById(R.id.output)
//call setText
output.setText(textoEuros);
In one of my screen i am using text watcher to check value against a range and for invalid range color of text is being changed.
Now there is one more requirement.
Along with EditText i need to use CheckBox to set completely different range.
The problem i am facing is that changing the checkbox value requires to call afterTextChanged in which i have put all validations for both set of ranges.
So basically my requirement is to update textWatcher anyhow so that afterTextChanged get called after i change value of checkbox.
I get a strange feeling that i am forgetting something very simple here,if so please let me know.
Thanks in advance.
Perhaps move your code from the callback in the textWatcher and put it into another function ie
private void checkRange() {
// do checks here
String color = text.getEditableText().toString();
}
and call this method from your textWatcher and from the listener on the checkbox.
In my application I have a list of questions stored in an ArrayList, and I want to display a dialog that shows one question, and then continues to the next one after the question is answered. The way that I'm currently doing it (iterating through a loop) hasn't been working because it just layers all of the dialogs on top of one another all at once which causes a host of other issues. What I'm looking for is a way to still iterate through the questions, but just change the layout of the dialog each time until it has finished each question in the list. Can anyone give me a good pointer for how to get this going?
You can make a function that takes title and message as parameters and shows a dialog.
showDialog(String title, String message){ // Show dialog code here}
Within that dialog's answer button's listener call another function (showQuestion(currentQuestion)) that iterates the arrayList till it is over
int currentQuestion=0;
ArrayList<QuestionObject> questionList;
showQuestion(int i){
if(i<questionList.size()){
showDialog(questionList.get(i).getTitle,questionList.get(i).getMessage);
currentQuestion++;
}else{
//quiz is over
}
}
I assume you mean that you just want to change 1 single layout(created within XML i.e main.xml). In order to do this, make sure that the class your working on is pointing to that layout. From there (assuming your using an Event listener for when the user submits an answer) you can change do as you want by the following:
TextView txt = (TextView) findViewById(R.id.textView); // references the txt XML element
and in your Event listener, if the answer is correct then change(Have i be a global variable thats initially set to 0).
if(i<arrayList.size()){
txt.setText(arrayList.get(++i));
}else{
txt.setText("You Finished");
}
From there, in the else statement, you can change arrayLists and reset i to 0;
If you are trying to use the positive, neutral, and negative buttons; then you may have problems with multiple dialogs. Try defining a customized layout with your own TextViews, ListViews, and Buttons. You can implement listeners and everything else like a regular layout. Then just pass your customized layout to the dialog through AlertDialog.Builder.setView().
PS If you include code examples of what you are currently doing we can provided answers that are less vague.
Let me explain:
I need to show the name of any building block either it is imagebutton, edittext in my textview field depending upon which of above written will be hover over by the user.
So that my textview could behave like some dynamic display plate.
Any help will highly be appreciated.
mrana..
So something that you can do. Since there it no "setText" for imageviews, you can do something like
String name = "imageview";
imageView.setTag(name);
Then in your onFocusedChangedListener call the following method
void displayInTextView(View selectedView) {
String viewName = (String) selectedView.getTag();
mDisplayText.setText(viewName);
}
Since this is a touch device, "hovering" will not be possible. One solution is to show the name on when long-press. See this solution https://stackoverflow.com/a/4433441/1227692
EDI: Thanks Frank and mrana for pointing out. I agree and take back my comment.
I am trying to make a score increment when an answer is shown as correct in a textfield. The code is as follows:
Variable is declared:
static int score = 0;
Method:
public void score(){
if(check.getText().equals("Correct")){
score++;
}
Score.setText(String.valueOf(score));
}
The Score is a textfield and check is another textfield
Then this is called in onclick as score();
I appreciate any ideas on why this gets a process closed error when I am clicking the button.
Thanks.
Edit:
Best advice I can give is set a break point at your if statement then use the debugger to see what is null.