2012-07-31 24 views
2

我已經爲每個項目分割了包含RadioButton的自定義ListView。當一個對話框打開時,已經被選中(選中)的RadioButton變爲未被選中(未被選中)。我不知道什麼可能會導致這種情況發生......ListView中的RadioButton在Dialog出現後取消選中

這是我的代碼清單。

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; 
} 
+0

那麼,如果你至少沒有發佈你的代碼,那麼人們不會知道。 – 0gravity 2012-07-31 03:59:57

+1

確定我已經發布了我的代碼:D – 2012-07-31 04:04:55

回答

1

好吧,我想我知道解決方案。這是因爲我把notifyDataSetInvalidate放在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); 
    } 
2

在這段代碼中你只顯示在兩種情況下的對話框,在投1和8 但你不改變單選按鈕,在這兩種情況下的選擇。 所以請致電radioButton.setChecked(true);也。

+1

上面的列表視圖被分區,第1項和第8項都沒有radioButton。 – 2012-07-31 04:30:21

相關問題