単一の選択肢でカスタムAlertDialogを作成する必要がありますが、ラジオボタンはなく、各アイテムに2つのカスタム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が望ましいと思います。しかし、それを作る方法は?