在Dialog出现后,ListView中的RadioButton未选中

时间:2012-07-31 03:57:18

标签: android android-listview radio-button alertdialog

我已经对每个项目包含RadioButton的自定义ListView进行了分区。打开对话框后,已选中(已选中)的RadioButton将取消选中(未选中)。我不知道会发生什么事情......

这是我的代码清单。

ArrayList<Item> items = new ArrayList<Item>();
private EntryAdapter adapter;
private SharedPreferences preference;
private int usernameKe;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    preference = getSharedPreferences("preference", Context.MODE_PRIVATE);

    items.add(new SectionItem("Pengaturan PIN"));
    items.add(new EntryItem("Ubah PIN",
            "Masukkan PIN Anda yang baru di sini"));

    items.add(new SectionItem("Pilihan Nomor Tujuan Pengiriman"));
    items.add(new EntryItem("IM3", "085741222225"));
    items.add(new EntryItem("AS", "082372423333"));
    items.add(new EntryItem("XL", "081915348000"));
    items.add(new EntryItem("3", "08986903333"));

    items.add(new SectionItem("Pengelolaan User ID"));
    items.add(new EntryItem("Tambah ID", "Masukkan User ID baru"));
    int jumlahUsername = Integer.parseInt(preference.getString(
            "jumlah_userid", "0"));
    if (jumlahUsername >= 1) {
        for (int i = 1; i <= jumlahUsername; i++) {
            String nilaiDitampilkan = preference.getString("u" + i, "");
            items.add(new EntryItem(nilaiDitampilkan, ""));
        }
    }
    // Toast.makeText(this, jumlahUsername + "", Toast.LENGTH_SHORT).show();

    int nomorTerpilih = preference.getInt("nomor", 0);

    adapter = new EntryAdapter(QuickPrefsActivity.this, this, items,
            nomorTerpilih);
    setListAdapter(adapter);
}

@Override
protected void onListItemClick(ListView l, View v, int selectedPosition,
        long id) {

    if (!items.get(selectedPosition).isSection()) {
        RadioButton radioButton = (RadioButton) v
                .findViewById(selectedPosition);

        ((MyApplication) getApplication())
                .setSomeVariable(selectedPosition);
        Editor editorPage = preference.edit();
        editorPage.putInt("nomor", selectedPosition);
        editorPage.commit();

        switch (selectedPosition) {
        case 1:
            showDialog(0);
            break;
        case 3:
            radioButton.setChecked(true);
            break;
        case 4:
            radioButton.setChecked(true);
            break;
        case 5:
            radioButton.setChecked(true);
            break;
        case 6:
            radioButton.setChecked(true);
            break;
        case 8:
            showDialog(1);
        }
        adapter.notifyDataSetInvalidated();
    }
    super.onListItemClick(l, v, selectedPosition, id);
}

@Override
protected Dialog onCreateDialog(int id) {
    Dialog dialog = null;
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View layout = inflater.inflate(R.layout.ubah_pin_dialog,
            (ViewGroup) findViewById(R.id.ubahPinLayout));
    final TextView textView = (TextView) layout
            .findViewById(R.id.upahPinTextView);
    textView.setGravity(Gravity.CENTER);
    final EditText editText = (EditText) layout
            .findViewById(R.id.pinEditText);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(layout);
    if (id == 0) {
        String pin = preference.getString("PIN", "");
        textView.setText("Masukkan PIN Anda yang baru di sini");
        editText.setText(pin);
        editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        builder.setTitle("Ubah PIN");
        builder.setPositiveButton("Ubah",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        String pinDisimpan = editText.getText().toString();
                        if (pinDisimpan.length() == 4) {
                            SharedPreferences pinPreference = getSharedPreferences(
                                    "preference", Context.MODE_PRIVATE);
                            Editor editorPage = pinPreference.edit();
                            editorPage.putString("PIN",
                                    pinDisimpan.toString());
                            editorPage.commit();
                        } else {
                            Toast.makeText(getApplicationContext(),
                                    "GAGAL: PIN tidak valid",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });
        builder.setNegativeButton("Batal",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                    }
                });
        final AlertDialog pinDialog = builder.create();
        dialog = pinDialog;
    } else {
        textView.setText("Masukkan username Anda yang baru di sini");
        editText.setText("");
        builder.setView(layout);
        builder.setTitle("User ID");
        builder.setPositiveButton("Tambah",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        usernameKe = items.size() - 8;
                        String size = String.valueOf(usernameKe);
                        String nilaiDisimpan = editText.getText()
                                .toString();
                        preference = getSharedPreferences("preference",
                                Context.MODE_PRIVATE);
                        Editor editorPage = preference.edit();
                        editorPage.putString("u" + size, nilaiDisimpan);
                        editorPage.putString("jumlah_userid", size);
                        editorPage.commit();

                        Toast.makeText(getApplicationContext(),
                                Integer.parseInt(size) + "",
                                Toast.LENGTH_SHORT).show();
                        items.add(new EntryItem(nilaiDisimpan, ""));
                        adapter.notifyDataSetChanged();
                    }
                });
        builder.setNegativeButton("Batal",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                    }
                });
        final AlertDialog pinDialog = builder.create();
        dialog = pinDialog;
    }
    return dialog;
}

2 个答案:

答案 0 :(得分:2)

在此代码中,您仅在演员表1和8的两种情况下显示对话框。 但在这两种情况下,您不会更改单选按钮的选择。 所以也请致电radioButton.setChecked(true);

答案 1 :(得分:1)

好吧,我想我知道解决方案。这是因为我把switchDataSetInvalidate放在switch case(listView items)之外。它适用于所有情况。因此,代码应该如下所示进行更改。

switch (selectedPosition) {
    case 1:
        showDialog(0);
        break;
    case 3:
        radioButton.setChecked(true);
    adapter.notifyDataSetInvalidated();
        break;
    case 4:
        radioButton.setChecked(true);
    adapter.notifyDataSetInvalidated();
        break;
    case 5:
        radioButton.setChecked(true);
    adapter.notifyDataSetInvalidated();
        break;
    case 6:
        radioButton.setChecked(true);
    adapter.notifyDataSetInvalidated();
        break;
    case 8:
        showDialog(1);
    }