简体   繁体   中英

Radiobutton not remembering previously selected item Android

The output of the below code is a Image Button, when we click on the Image button a alertdialog box with two radio buttons are shown, to select between 2 different tracks. Everything works fine but when i click the Image button again ie second time, the radio buttons dose not shows the currently playing track ie previously selected track. Previously selected item is not remembered. ?

public class MainActivity extends Activity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b1=(Button) findViewById(R.id.button1);
    final CharSequence[] items = {"Track #1", "Track #2"};

final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a number");
builder.setIcon(R.drawable.ic_launcher);
builder.setNeutralButton("OK",
    new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
            // Write your code here to execute after dialog
            // closed
            Toast.makeText(getApplicationContext(), "You clicked on OK",
                    Toast.LENGTH_SHORT).show();
            dialog.cancel();
        }
    });
    builder.setSingleChoiceItems(items, -1,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int item) {
                    // TODO Auto-generated method stub
                    switch (item) {
                    case 0:
                        Toast.makeText(getApplicationContext(),
                                items[item], Toast.LENGTH_SHORT).show();
                        // Your code when first option seletced
                        break;
                    case 1:
                        // Your code when 2nd option seletced
                        Toast.makeText(getApplicationContext(),
                                items[item], Toast.LENGTH_SHORT).show();
                        break;

                    }
                    dialog.cancel();
                }
            });
    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            AlertDialog alert = builder.create();
            alert.show();
        }
    });
}
}

Save the index of the choice when user changes it. Pass this on to builder.setSingleChoiceItems() :

public class MainActivity extends Activity {
    int currentChoice = -1;
    Button b1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1=(Button) findViewById(R.id.button1);
        final CharSequence[] items = {"Track #1", "Track #2"};

        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Pick a number");
        builder.setIcon(R.drawable.ic_launcher);
        builder.setNeutralButton("OK",
            new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // Write your code here to execute after dialog
                // closed
                Toast.makeText(getApplicationContext(), "You clicked on OK",
                Toast.LENGTH_SHORT).show();
                dialog.cancel();
            }
        });

    builder.setSingleChoiceItems(items, currentChoice,
        new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                // TODO Auto-generated method stub
                switch (item) {
                case 0:
                    Toast.makeText(getApplicationContext(),
                            items[item], Toast.LENGTH_SHORT).show();
                    // Your code when first option seletced
                    currentChoice = 0;
                    break;
                case 1:
                    // Your code when 2nd option seletced
                    Toast.makeText(getApplicationContext(),
                            items[item], Toast.LENGTH_SHORT).show();
                    currentChoice = 1;
                    break;

                }
                dialog.cancel();
            }
        });
    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            AlertDialog alert = builder.create();
            alert.show();
        }
    });
}

}

You just need to take a new variable named

public int mSelectedValue = -1;

and pass it to

builder.setSingleChoiceItems(items, mSelectedValue, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialmSelectedValue og, int item) {
                switch (item) {
                case 0:
                    mSelectedValue = 0;
                    Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    mSelectedValue = 1;
                    Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                    break;
                }
                dialog.cancel();
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM