我需要使用单选制作自定义 AlertDialog,但没有单选按钮,并且每个项目中有两个自定义 TextView。我尝试使用 AlertDialog:
ArrayList<HashMap<String,String>> items=new ArrayList<HashMap<String,String>>();
//..here I fill my ArrayList
SimpleAdapter simpleAdapter=new SimpleAdapter(this, items, R.layout.list_item, new String[] {"name","count"}, new int[] {R.id.name,R.id.count});
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setSingleChoiceItems(simpleAdapter, -1, new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
//Here I handle click
}});
alert.show();
但是点击项目后它不会关闭。为什么?可能我可以修复它吗?
或者,我尝试使用 Dialog :
ArrayList<HashMap<String,String>> items=new ArrayList<HashMap<String,String>>();
//..here I fill my ArrayList
SimpleAdapter simpleAdapter=new SimpleAdapter(this, items, R.layout.list_item, new String[] {"name","count"}, new int[] {R.id.name,R.id.count});
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
View view=LayoutInflater.from(this).inflate(R.layout.items_list, null);
ListView listView=(ListView) view.findViewById(R.id.list_view);
listView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
//Here I handle click
dialog.dismiss();
}});
dialog.setContentView(view);
dialog.show();
但它有样式问题,文本不显示(文本颜色与背景颜色一致)。
我认为 AlertDialog 更适合我。但是如何实现呢?