アクティビティ (メイン) があり、それにボタンを挿入しました。ユーザーがボタンを押すと、2 つのラジオ ボックスを含むダイアログ ボックスが表示されます。どのラジオボタンが選択されているかに基づいて「1」または「0」の値を「ntv」に設定し、メインアクティビティで「ntv」値を使用したいのですが、これは「ntv」値をメインアクティビティに転送しないようです。私のコードの何が問題なのですか?
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();
「ntv」を文字列として定義しました。これは、「ntv」を比較して「0」か「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); }
そして、(set1) も (set2) も見えなくなるわけではないので、「ntv」には価値がないことに気づきます。