2012-07-31 2 views
2

각 항목에 RadioButton이 포함 된 사용자 정의 ListView가 섹션화되어 있습니다. 대화 상자가 열리면 선택 (선택) 된 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; 
} 
+0

코드를 올리지 않으면 잘 알지 못할 것입니다. – 0gravity

+1

확인 코드를 게시했습니다 : D –

답변

1

나는 해결책을 안다고 생각한다. 왜냐하면 나는 switch case (listView items) 외부에 notifyDataSetInvalidate를 놓기 때문이다. 그것은 모든 경우에 적용됩니다. 따라서 코드는 아래와 같이 변경해야합니다.

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

위의 listView는 섹션으로 구분되어 있으며 항목 1과 항목 8에는 radioButton이 없습니다. –

관련 문제