So I'm trying to write an activity that has two spinners and a button, when the two spinners are selected and the button pressed it'll take you to another activity. Except for one combination, which should produce a Toast saying that you can't do this.
Anyway, this is the code:
public void onClick(View v) {
String spinnerchoice1 = ("spinner1Value");
String spinnerchoice2 = ("spinner2Value");
if((spinnerchoice1.equals("Walking")) && (spinnerchoice2.equals("Hiking"))){
Toast.makeText(getBaseContext(), "I'm sorry, this is not possible.", Toast.LENGTH_LONG).show();
}else{
Intent i = new Intent(GetDirections.this.getApplicationContext(), DirectionDisplay.class);
i.putExtra("spinner1Value", transportSpinner.getSelectedItem().toString());
i.putExtra("spinner2Value", locationSpinner.getSelectedItem().toString());
GetDirections.this.startActivity(i);
}
}
Can anyone tell me where I'm going wrong?
Thanks
You are comparing two hard-coded strings, the if condition will never execute. Change the code to:
public void onClick(View v) {
String transport = transportSpinner.getSelectedItem().toString();
String location = locationSpinner.getSelectedItem().toString();
if ("Walking".equals(transport) && "Hiking".equals(location)) {
Toast.makeText(getBaseContext(), "I'm sorry, this is not possible.", Toast.LENGTH_LONG).show();
} else {
Intent i = new Intent(GetDirections.this.getApplicationContext(), DirectionDisplay.class);
i.putExtra("spinner1Value", transport);
i.putExtra("spinner2Value", location);
GetDirections.this.startActivity(i);
}
}
If this is your actual code then your if is never going to evaluate to true because you are setting the strings to values that are not "Walking" and "Hiking"
these two lines:
String spinnerchoice1 = ("spinner1Value");
String spinnerchoice2 = ("spinner2Value");
need to be something like this (assuming that you spinner just contains String objects and not some other type):
String spinnerchoice1 = transportSpinner.getSelectedItem().toString();
String spinnerchoice2 = locationSpinner.getSelectedItem().toString();
Related
I would like to compare the Id that the user has inputted with the Id from the database as a login process. However, when I run the code after filling all the ID and password, the application returns that inputted ID and inputted pw are empty. What's the problem?
It happens because
final String inputId = editId.getText().toString();
final String inputPw = editPw.getText().toString();
These strings are initialized at the beginning in the onCreate() method.
When you click the button the buttonLogIn.setOnClickListener is executed with the same values initialized (empty values).
You have to get the new values from the EditText views.
buttonLogIn.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
//...
String user = editId.getText().toString();
String psw = editPw.getText().toString();
//..
if (!TextUtils.isEmpty(user)&& !TextUtils.isEmpty(psw)){ ... }
}
});
As mentioned by #Andy in the comments, you have to change also the check on firebase because the method is asynchronous.
if (TextUtils.isEmpty(user)&& TextUtils.isEmpty(psw)){
Toast.makeText(LoginScreen.this, "Complete all fields", Toast.LENGTH_SHORT).show();
} else {
//Firebase check
....
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
//....
//Check the values here
if (userId.equals(inputId)) && userPw.equals(inputPw)){...}
}
}
You need to get the value from the edit text after the text has been updated:
if (!TextUtils.isEmpty(editId.getText().toString())&& !TextUtils.isEmpty(editPw.getText().toString()))...
Does anyone know what's wrong with my code, I'm trying to compare the input in my edit text field to ensure their equal value before it creates an account.
//compare the fields contents
if(editTextPassword.equals(editTextReEnterPassword)) {
//they are equal
//Creating a new user
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
finish();
startActivity(new Intent(getApplicationContext(), Home.class));
} else{
Toast.makeText(Register.this, "Could not register... please try again", Toast.LENGTH_SHORT).show();
}
}
});
}else {//they are not equal
Toast.makeText(Register.this, "Passwords do not match... please try again", Toast.LENGTH_SHORT).show();
}
You have to get the text value of the edit text.
So, really, you can not do editText.equals()
You would do something like this..
String editTextPasswordString = editTextPassword.getText().toString().trim();
String editTextReEnterPasswordString = editTextReEnterPassword.getText().toString().trim();
if(editTextPasswordString.equals(editTextReEnterPasswordString)) {
//code
}
First you need to get the value from EditText using the method getText().toString(), like this:
String newPassword= editTextReEnterPassword.getText().toString();
For more information about the method, please check the follow link:
Get Value of EditText
After that, you need to compare the previous string editTextPassword with the new one created, which is the result of the method getText(), editTextReEnterPassword.
So your final code, should be like this:
String newPassword = editTextReEnterPassword.getText().toString();
String atualPassword = editTextPassword.getText().toString();
if(atualPassword.equals(newPassword))
{
...
}
else
{
...
}
Try:
if(editTextPassword.getText().toString().equals(editTextReEnterPassword.getText().toString()))
EditText is not a String
Hello everyone so i'm pretty new to Java and this code below will surely prove it.
I'd greatly appreciate any tip upon where i've gone wrong, thanks!
I'm trying to create a simple app that displays a random quote, and gives the chance to guess who said it.
Pretty sure the main issue has to do with ---> if (guessNameInt == whoSaidItInt)
because it only crashes when i click the button which enables the tryLuck if statement.
Heres my code below
int randomNum;
String whoSaidIt;
// quotes and numbers
public void randomRick(View view) {
if (randomNum == 0) {
Toast.makeText(getApplicationContext(), "Life is effort and I'll stop when I die!", Toast.LENGTH_LONG).show();
whoSaidIt = "Jerry";
}
if (randomNum == 1) {
Toast.makeText(getApplicationContext(), "Well look where being smart got you.", Toast.LENGTH_LONG).show();
whoSaidIt = "Jerry";
}
if (randomNum == 2) {
Toast.makeText(getApplicationContext(), "Ohh yea, you gotta get schwifty.", Toast.LENGTH_LONG).show();
whoSaidIt = "Rick";
}
}
public void tryLuck(View view) {
EditText guessedName = (EditText) findViewById(R.id.authorIs);
String guessedNameString = guessedName.getText().toString();
int guessNameInt = Integer.parseInt(guessedNameString);
int whoSaidItInt = Integer.parseInt(whoSaidIt);
if (guessNameInt == whoSaidItInt) {
Toast.makeText(getApplicationContext(), "Holy crow, Good job!", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(), "Try again", Toast.LENGTH_LONG).show();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Random randomGenerator = new Random();
randomNum = randomGenerator.nextInt(3);
}`
You're trying to convert a String to an Int. This will only work if the String is an actual int (ie a number), and not the name (Jerry or Rick). Use guessNameString.equals(whoSaidIt) instead.
I know this has got to be simple. But for the life of me i don't know why i can't get this right.
Ok so I want to go from a listview page (got that) then click a switch to make it go to the next page (also got that.) Then I want a int to tell me which position I am on form the last page (might be working?) now i can't get the If Else statement to work in the page.
public class NightmareParts extends Activity
{
public int current_AN_Number = NightmareList.AN_position_num;
private TextView edit_title;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.part_layout);
// isn't working here. Why?
// test_edit = (TextView)findViewById(R.id.directions_tv);
// test_edit.setText(R.string.directions_text_two);
// works without this being in here.
setDoneButtonListener();
}
//Set up the Done button to initialize intent and finish
private void setDoneButtonListener()
{
Button doneButton = (Button) findViewById(R.id.back_button);
doneButton.setOnClickListener (new View.OnClickListener() {
#Override
public void onClick(View v)
{
finish();
}
});
}
private void editTitle()
{
if (current_AN_Number = 1)
{
edit_title = (TextView)findViewById(R.id.part_title);
edit_title.setText(R.string.AN_title_1);
}
}
}
The current_AN_number is coming from the last page.
Your if statement is incorrect:
if (current_AN_Number = 1)
You've used the assignment operator, when you wanted to compare it with the == operator:
if (current_AN_Number == 1)
if (current_AN_Number = 1)
Should be
if (current_AN_Number == 1)
You're not setting current_AN_Number to be 1, you are comparing if it is equal to 1. So use ==.
test_edit = (TextView)findViewById(R.id.directions_tv);
is not working because test_edit is never declared.
I'm trying to perform a check on some information in a database. If i run the following code without it being in a loop it runs fine, but only checking the first row, what i need it to do is to check the names and dates for each row.
If i understand the while loop correctly it would move my cursor to the next row then just run the code again. Can anyone see why this is looping until my program crashes?
while (cursor.moveToNext()) {
String titlefromdb = cursor.getString(3);
if (strTitle.equals(titlefromdb)&& cursor.getString(1).equals(dateselforap)) {
Log.d("insidematch", "date and title matched");
final Dialog matchdiag = new DialogCW2Organisor.this);
matchdiag.setContentView(R.layout.apptmatch);
matchdiag.setTitle("View/Edit Appointment");
matchdiag.setCancelable(true);
TextView matchtxt = (TextView) matchdiag.findViewById(R.id.matchtxt);
matchtxt.setText("Appointment \""+ titlefromdb + "\" already exists, please choose a different event title");
Button btnmatchok = (Button) matchdiag.findViewById(R.id.btnmatch);
btnmatchok.setOnClickListener(new OnClickListener() {
//on click for cancel button
#Override
public void onClick(View v) {
matchdiag.dismiss();}
});
matchdiag.show();
} else {
addAppt(strTime, strTitle, strDet);
cursor = getAppts();
dialog.dismiss();
}
}
Try moving to the first record before calling moveToNext().
Move your functionality into a do/while loop so you can still grab the first record
if (!cursor.moveToFirst())
return; //nothing to do since the cursor is empty
do
{
String titlefromdb = cursor.getString(3);
if (strTitle.equals(titlefromdb)&& cursor.getString(1).equals(dateselforap)) {
Log.d("insidematch", "date and title matched");
final Dialog matchdiag = new DialogCW2Organisor.this);
matchdiag.setContentView(R.layout.apptmatch);
matchdiag.setTitle("View/Edit Appointment");
matchdiag.setCancelable(true);
TextView matchtxt = (TextView) matchdiag.findViewById(R.id.matchtxt);
matchtxt.setText("Appointment \""+ titlefromdb + "\" already exists, please choose a different event title");
Button btnmatchok = (Button) matchdiag.findViewById(R.id.btnmatch);
btnmatchok.setOnClickListener(new OnClickListener() {
//on click for cancel button
#Override
public void onClick(View v) {
matchdiag.dismiss();
}
});
matchdiag.show();
} else {
addAppt(strTime, strTitle, strDet);
cursor = getAppts();
dialog.dismiss();
}
} while (cursor.moveToNext());
I have also run into the infinite loop problem, which really baffled me as well, since a while !moveToNext() loop should definitely finish.
However, the workaround is to use a for loop over the length of the cursor, and process each cursor.moveToPosition(i).
for (int i = 0; i <= cursorLen; i++) {
if (!cursor.moveToPosition(i)) {
return;
}
// process your cursor
}
I feel like this must be a bug with the Cursor implementation, because a while loop over cursor.moveToNext() should always finish.