0

在這裏我創建了自定義對話框,其中包括單選模式的列表視圖。當用戶在列表視圖中選擇其中一個項目時,應在下次打開對話框時選擇它,並且用戶還必須能夠選擇另一個項目。 Butmy問題是當對話框重新打開時對話單選按鈕未被選中。我已經回答了http://blog.thisisfeifan.com/2011/10/2-lines-text-in-single-choice-listview.htmlListView項目單擊打開與另一個自定義列表視圖的自定義對話框

那麼我的代碼有什麼問題?

我定製對話框的XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout android:id="@+id/relativeLayout1" 
     android:layout_width="250dp" 
     android:layout_height="50dp"  
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:background="@drawable/list_selector"> 

    <LinearLayout android:layout_centerVertical="true" 
     android:layout_width="250dp" 
     android:layout_height="50dp" 
     android:layout_alignParentLeft="true" 
     android:orientation="vertical" android:gravity="center_vertical"> 

    <TextView android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:id="@+id/tv_MainText" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:paddingLeft="12dp" /> 
    </LinearLayout> 

    <RadioButton android:layout_height="wrap_content" 
     android:id="@+id/rb_Choice" android:layout_width="wrap_content" android:layout_centerVertical="true" 
     android:layout_alignParentRight="true" android:gravity="center_vertical" 
     android:focusable="false" android:clickable="false" 
     android:focusableInTouchMode="false" 
     android:paddingRight="12dp"> 
     </RadioButton> 

</RelativeLayout> 

自定義對話框列表視圖文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="250dp" 
    android:layout_height="210dp"> 

    <LinearLayout 
     android:id="@+id/layout_btn" 
     android:layout_width="fill_parent" 
     android:layout_height="3dp" 
     android:gravity="center">   
     <ImageView 
      android:src="@drawable/separator" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_above="@+id/dialoglist" 
     />   
    </LinearLayout> 
    <ListView 
     android:id="@+id/dialoglist" 
     android:layout_width="250dp" 
     android:layout_height="215dp" 
     android:divider="#242424" 
     android:dividerHeight="2dp" 
     android:listSelector="@drawable/list_selector" 
     android:paddingTop="5dp" 
     /> 
    </RelativeLayout> 

在我有ListView和列表項我的主要活動點擊我已經打開自定義對話框:

lv.setOnItemClickListener(new OnItemClickListener() { 

      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 

       if(position == 1) 
       { 
        final Dialog dialog = new Dialog(context,R.style.CustomDialogTheme); 
        dialog.setContentView(R.layout.dialog_layout); 


        dialog.setTitle("Select Font Size"); 

        final String[] sizeType = new String[] {"Normal" , "Medium" , "Large" , "Extra Large"}; 
        final ArrayList<HashMap<String, Object>> m_data = new ArrayList<HashMap<String, Object>>(); 
        final ArrayList<HashMap<String, Object>> m_data_new = new ArrayList<HashMap<String, Object>>(); 

        final HashMap<String, Object> data_new = new HashMap<String, Object>(); 
        for(int i = 0; i < sizeType.length;i++) 
        { 
         HashMap<String, Object> data = new HashMap<String, Object>(); 
         data.put("item1",sizeType[i]); 

         m_data.add(data); 

        } 

         for (HashMap<String, Object> m :m_data) //make data of this view should not be null (hide) 

         { 
           m.put("checked", false); 

         } 

        final ListView lst = (ListView) dialog.findViewById(R.id.dialoglist); 
        //lst.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

        final SimpleAdapter adapter = new SimpleAdapter(context, m_data, R.layout.dialoglist_item,new String[] {"item1","checked"},new int[] {R.id.tv_MainText,R.id.rb_Choice}); 
        lst.setAdapter(adapter); 

        lst.setOnItemClickListener(new OnItemClickListener() { 
         public void onItemClick(AdapterView<?> arg0, View arg1, 
           int item, long arg3) { 
          RadioButton rb = (RadioButton) dialog.findViewById(R.id.rb_Choice); 
          if (!rb.isChecked()) //OFF->ON 
          {      
           for (HashMap<String, Object> m :m_data) //clean previous selected 
            m.put("checked", false); 

           m_data.get(item).put("checked", true);    
           adapter.notifyDataSetChanged(); 


          }  

          selectedSize = sizeType[item]; 
          //dialog.dismiss(); 

         } 

        }); 
        dialog.show(); 
       } 

所以請解決我的問題。提前Thanx。

+0

我無法得到這應該如何工作的句柄?會不會要求提供對話圖片?與單選按鈕? – mango

+0

它似乎與單選按鈕的警報對話框類似。 – zanky

+0

那麼你想要保存什麼?如果1按鈕被點擊或沒有? – mango

回答

0

對於簡單地保存價值的持續應用會話可以使用SharedPreferences API: 這裏有保存的樣品:

SharedPreferences settings = getSharedPreferences("MYPREFS", 0); 
    SharedPreferences.Editor editor = settings.edit(); 
    editor.putBoolean("isChecked", rb.isChecked()); 
    editor.commit(); 

其中rb是你的單選按鈕。並檢索,並在下一次應用它,你可以使用:

SharedPreferences settings = getSharedPreferences("MYPREFS", 0); 
    rb.setChecked(settings.getBoolean("isChecked", false)); 

注意false這裏簡直就是萬一默認值與該鍵不存在sharedPrefs文件尚未值。如果不存在,它會自動創建一個。

+0

ohk。感謝您的幫助。我會嘗試.. – zanky

相關問題