Reputation: 3374
I have an activity (Main) and I inserted a button in it. When button the user press it, a dialog box with 2 Radio boxes appear. I want to set "1" or "0" value to "ntv", based on which radiobutton is selected, and then use "ntv" value in Main activity, but it seems that this doesnot transfer "ntv" value to Main activity, what is wrong with my code?
final CharSequence[] chan = {"Minutes", "Seconds"}; builder = new AlertDialog.Builder(Main.this); builder.setTitle("Please Select:"); builder.setSingleChoiceItems(chan, 0, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { if(chan[item]=="Minutes") { Toast.makeText(getApplicationContext(), "Minutes", Toast.LENGTH_SHORT).show(); ntv="1"; } else if (chan[item]=="Seconds") { Toast.makeText(getApplicationContext(), "Seconds", Toast.LENGTH_SHORT).show(); ntv="0"; } } }); AlertDialog alert = builder.create(); alert.show();
I defined "ntv" as string and this is part of code when "ntv" is compared to check if it is "0" or "1"
ImageView set1= (ImageView) findViewById(R.id.set1); ImageView set2= (ImageView) findViewById(R.id.set2); if (ntv.equals("0")) { set1.setVisibility(View.INVISIBLE); } if (ntv.equals("1")) { set2.setVisibility(View.INVISIBLE); }
and because neither (set1) nor (set2) doesnot go invisible I realize that "ntv" have no value.
Upvotes: 0
Views: 781
Reputation: 95578
This all looks OK (except the suggestion to use equals()
instead of ==
for the string compares, although, as you say, it does work (it just isn't good practice).
The only thing I can think of (without seeing all the code) is that the scope of variable ntv
is wrong. Have you declared the variable inside a method? It needs to be defined as an instance variable in your class (ie: not within a method).
Upvotes: 1
Reputation: 2229
it's not clear the complete code you use and how you call the code that change visibility. Below an example
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder builder;
final CharSequence[] chan = {"Minutes", "Seconds"};
builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Please Select:");
builder.setSingleChoiceItems(chan, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if(chan[item].equals("Minutes")) {
showToast("Minutes");
} else if (chan[item].equals("Seconds")) {
showToast("Seconds");
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
private void showToast(String s){
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}
instead of showToast function you can use a your function to change visibility
Upvotes: 0
Reputation: 13855
you should be doing .equals
on the string comparison NOT ==
It is unlikely that your if statements will trigger because of this.
if(chan[item].equals("Minutes"))
{
Toast.makeText(getApplicationContext(), "Minutes", Toast.LENGTH_SHORT).show();
ntv="1";
}
else if (chan[item].equals("Seconds"))
{
Toast.makeText(getApplicationContext(), "Seconds", Toast.LENGTH_SHORT).show();
ntv="0";
}
Upvotes: 1