选择题AlertDialog自定义适配器 [英] Multiple choice AlertDialog with custom Adapter

查看:194
本文介绍了选择题AlertDialog自定义适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个AlertDialog与选择题选项。我曾尝试与 setMultiChoiceItems 但是,我有一个的ArrayList<类别> ,而不是的CharSequence ,所以我试图与该适配器。

setAdapter 的问题是,当我选择一个项目它会关闭对话窗口。而我要的是选择的项目,然后点击确定按钮,看看哪些项目选择在哪里。

  AlertDialog.Builder建设者=新AlertDialog.Builder(本);
        builder.setTitle(选择颜色);
        ArrayAdapter<类别> catsAdapter =新的ArrayAdapter<类别>(这一点,android.R.layout.select_dialog_multichoice,this.categories);
        builder.setAdapter(catsAdapter,新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话,诠释项){

            }
        });
        builder.setPositiveButton(保存,新DialogInterface.OnClickListener(){
               @覆盖
               公共无效的onClick(DialogInterface对话,诠释它){
                //做一点事
               }
        });;

        AlertDialog警报= builder.create();
        alert.show();
 

解决方案

不幸的是,似乎没有成为一个简单的方法来切换AlertDialog的multichoicemode,而无需调用 setMultiChoiceItems()

不过,你可以设置一个适配器,然后打开multichoice模式中包含的ListView 本身。

 最后AlertDialog对话框=新AlertDialog.Builder(getActivity())
    .setTitle(标题)
    .setAdapter(yourAdapter,空)
    .setPositiveButton(getResources()的getString(R.string.positive),空)
    .setNegativeButton(getResources()的getString(android.R.string.cancel),空)
    。创建();

。dialog.getListView()setItemsCanFocus(假);
。dialog.getListView()setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
dialog.getListView()。setOnItemClickListener(新OnItemClickListener(){
    @覆盖
    公共无效onItemClick(适配器视图<>母公司视图中查看,
        INT位置,长的id){
        //这里管理选定项目
        的System.out.println(点击+位置);
        CheckedTextView的TextView =(CheckedTextView)视图;
        如果(textView.isChecked()){

        } 其他 {

        }
    }
});

dialog.show();
 

I am trying to create a AlertDialog with multiple choice option. I have tried with the setMultiChoiceItems but what i have is a ArrayList<Category> and not a CharSequence so i tried with the adapter.

The problem with setAdapter is that when i select one item it closes the dialog window. And what i want is to select the items and then hit the OK button to see what items where selected.

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Pick a color");
        ArrayAdapter<Category> catsAdapter = new ArrayAdapter<Category>(this, android.R.layout.select_dialog_multichoice,this.categories);
        builder.setAdapter(catsAdapter, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {

            }
        });
        builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                //do something  
               }                
        });;

        AlertDialog alert = builder.create();
        alert.show();

解决方案

Unfortunately, there doesn't seem to be an easy way to toggle on AlertDialog's multichoicemode without calling setMultiChoiceItems().

However, you can set an adapter, then turn on multichoice mode in the contained ListView itself.

final AlertDialog dialog = new AlertDialog.Builder(getActivity())
    .setTitle("Title")
    .setAdapter(yourAdapter, null)
    .setPositiveButton(getResources().getString(R.string.positive), null)
    .setNegativeButton(getResources().getString(android.R.string.cancel), null)
    .create();

dialog.getListView().setItemsCanFocus(false);
dialog.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
dialog.getListView().setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
        // Manage selected items here
        System.out.println("clicked" + position);
        CheckedTextView textView = (CheckedTextView) view;
        if(textView.isChecked()) {

        } else {

        }
    }
});

dialog.show();

这篇关于选择题AlertDialog自定义适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆