Reputation: 111
Got a problem here on exiting onclick.
The program uses shared preferences to get inputs,
and if String name = "User"
, the program wouldn't let the user click this button and just display an alert dialog box.
This is my code below:
public void onClick(View v) {
name = shared.getString("sharedname", "User");
gender = shared.getInt("sharedgender", 0);
age = shared.getInt("sharedage", 0);
weight =shared.getInt("sharedweight", 0);
height = shared.getInt("sharedheight", 0);
if(name=="User")
{
new AlertDialog.Builder(cont)
.setMessage("Please input your information first")
.setNeutralButton("OK", null).show();
break;//error
}
//code code code, rest of the code to be cancelle
Upvotes: 0
Views: 158
Reputation: 13327
use equals(Object)
method instead of ==
operator
if(name.equals("User"))
{
new AlertDialog.Builder(cont)
.setMessage("Please input your information first")
.setNeutralButton("OK", null).show();
return ;
}
The equals() method of java.lang.Object acts the same as the == operator; that is, it tests for object identity rather than object equality. The implicit contract of the equals() method, however, is that it tests for equality rather than identity. Thus most classes will override equals() with a version that does field by field comparisons before deciding whether to return true or false.
Upvotes: 3
Reputation: 369
AlertDialog.Builder builder = new Builder(ValueSelling.this);
AlertDialog alertDialog = builder.create();
alertDialog.setTitle(getResources().getString(R.string.termsTitle));
alertDialog.setMessage(getResources().getString(R.string.terms));
alertDialog.setCancelable(false);
alertDialog.setButton("Okay", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}});
alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
}});
alertDialog.show();
Upvotes: 0
Reputation: 11571
use :
if(name.equalsIgnoreCase("User")){
// your code
return;
}
Upvotes: 0