在这里,我创建了自定义对话框,其中包括具有单选模式的列表视图。当用户在列表视图中选择一个项目时,下次打开对话框时应该选择它,并且用户还必须能够选择另一个项目。但是我的问题是重新打开对话框时未选中对话框单选按钮。我已经参考了http://blog.thisisfeifan.com/2011/10/2-lines-text-in-single-choice-listview.html
那么我的代码有什么问题?
我的自定义对话框 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();
}
所以请解决我的问题。提前谢谢。