ListView项目单击使用另一个自定义列表视图打开自定义对话框

时间:2012-11-23 06:00:05

标签: android listview android-listview android-dialog customdialog

这里我创建了自定义对话框,其中包括带有单选模式的列表视图。当用户选择列表视图中的一个项目时,应该在下次打开对话框时选择它,用户也必须能够选择另一个项目。当重新打开对话框时,Butmy问题是对话框radiobutton未选中。我已经提到了http://blog.thisisfeifan.com/2011/10/2-lines-text-in-single-choice-listview.html

那我的代码有什么问题?

我的自定义Dialog 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();
                }

所以请解决我的问题。提前完成。

1 个答案:

答案 0 :(得分:0)

要简单地保存值以保留应用会话,您可以使用SharedPreferences API: 以下是保存示例:

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

其中rb是你的radioButton。并且要检索并在下次应用它,您可以使用:

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

请注意,false这里只是一个默认值,以防具有该键的值在sharedPrefs文件中不存在。如果不存在,它将自动创建一个。