【问题标题】:RadioButton inside ListView unchecked after Dialog appears出现对话框后未选中 ListView 内的 RadioButton
【发布时间】:2012-07-28 19:01:05
【问题描述】:

我已经对每个项目上包含 RadioButton 的自定义 ListView 进行了分段。当 Dialog 打开时,已选中(选中)的 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;
}

【问题讨论】:

  • 如果您至少不发布代码,人们也不会知道。

标签: android android-listview radio-button android-alertdialog


【解决方案1】:

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

【讨论】:

  • 上面的listView是分段的,第1项和第8项都没有radioButton。
【解决方案2】:

好的,我想我知道解决方案。这是因为我将 notifyDataSetInvalidate 放在 switch 案例(listView 项目)之外。它适用于所有情况。因此,代码应如下更改。

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);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-04
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 2011-08-25
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多