Reputation: 404
I don't think xml code is necessary, but here is the TextView
:
<TextView
android:id="@+id/option4"
android:layout_width="175dp"
android:layout_height="120dp"
android:background = "@drawable/rounded_corner"
android:textAppearance="?android:textAppearanceLarge"
android:gravity="center"
android:text="option 4"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/option3"
android:layout_toEndOf="@+id/option3"
android:screenOrientation="portrait"
android:onClick="onClick4"
android:clickable="true"/>
The last 2 lines are what detects the click.
My java code for that is:
public void onClick4(View v) {
if (option4Text.equals(Integer.toString(answer))) {
TextView questionText = (TextView) findViewById(R.id.question);
correctAnswer();
questionText.setText(questionTxt + "");
}
}
My code for option4Text in java class:
option4Text = (TextView)findViewById(R.id.option4);
option4Text.setText(wrongAnswer() + "");
Where wrongAnswer() just returns a random number. I think the problem is that option4Text itself isn't a string, so it doesn't have a value, after debugging I found that after clicking it is not going to inside of if statement. So my question is that, how do I get the text that I set for option4Text and put it into a String? what the way to use that string if if statement.
Upvotes: 0
Views: 3598
Reputation: 1291
Here problem is that option4Text is the object through which you are setting the string into your TextView instead of getting, to getting the TextView Value take anotehr String variable and get the value like:
String option4str;// take as global variable
option4Text = (TextView)findViewById(R.id.option4);
option4Text.setText(wrongAnswer() + "");
option4str = option4Text.getText().toString();
Then match like:
public void onClick4(View v) {
if (option4str .equals(Integer.toString(answer))) {
TextView questionText = (TextView) findViewById(R.id.question);
correctAnswer();
questionText.setText(questionTxt + "");
}
}
Upvotes: 0
Reputation: 1287
I couldn't Understand your problem clearly. But as I understand, You just want to fetch the string value of TextView from the xml file.
As I can see, you are doing some mistake. option4Text is an object of TextView. Therefore to fetch the String Value of it use the following code.
public void onClick4(View v)
{
if (option4Text.getText().toString().equals(toString(answer))) {
TextView questionText = (TextView) findViewById(R.id.question);
correctAnswer();
questionText.setText(questionTxt + "");
}
}
option4Text.getText().toString() will give the value of TextView which is set via Xml or by java code.
Upvotes: 2