我已经对每个项目上包含 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;
}