ListView Item Click Open Custom Dialog with Another Custom Listview

自古美人都是妖i 提交于 2019-12-11 20:17:21

问题


Here I have created custom dialog which includes listview with single choice mode. when user selects one of the item in listview then it should be selected when dialog is opened next time and user must be able to select another item also. Butmy problem is dialog radiobutton are unchecked when dialog is reopened. I have reffered the http://blog.thisisfeifan.com/2011/10/2-lines-text-in-single-choice-listview.html

So what is wrong in my code ?

My custom Dialog xml file:

<?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>

Custom Dialog Listview file :

<?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>

In my main activity I am having listview and on list item click i have opened the custom dialog :

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();
                }

So please solve my problem. Thanx in advance.


回答1:


For simply saving a value to persist app sessions you can use the SharedPreferences API: Here's a sample of saving:

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

where rb is your radioButton. and to retrieve it and apply it next time, you can use:

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

Note that false here is simply a default value in case a value with that key doesn't exist the sharedPrefs file yet. It will automatically create one if not existing.



来源:https://stackoverflow.com/questions/13523823/listview-item-click-open-custom-dialog-with-another-custom-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!