用一个ListView DialogInterface [英] DialogInterface with a ListView

查看:190
本文介绍了用一个ListView DialogInterface的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,我试图打开了一个的ListView 的有1 的TextView 和复选框。我的问题是单击该复选框时,可以说,在第2项,第6项也将被选中,而第10项和第14项,当所有我想要的是第2项中选择了一路。

我曾尝试:

  //设置列表适配器,然后设置就行了
ListAdapter listAdapter =新ArrayAdapter(PendingFriendsActivity.this,R.layout.activity_contact_list_item,R.id.contactNameTextView,ContactList);
AlertDialog.Builder builderSingle =新AlertDialog.Builder(PendingFriendsActivity.this);
DialogInterface.OnClickListener buttonClicked;
buttonClicked =新DialogInterface.OnClickListener(){
    @覆盖
    公共无效的onClick(DialogInterface对话,诠释它){
        切换(这){
            案例DialogInterface.BUTTON_POSITIVE:
                的for(int i = 0; I< sendContactList.size();我++)
                {
                    字符串手机,名称;
                    名称= sendContactList.get(ⅰ)获得(0);
                    电话= sendContactList.get(ⅰ)获得(1);
                    sendSMS(电话);
                }
                打破;            案例DialogInterface.BUTTON_NEGATIVE:
                //没有按钮点击
                打破;
        }
    }
};
builderSingle.setOnItemSelectedListener(新AdapterView.OnItemSelectedListener(){
    @覆盖
    公共无效onItemSelected(适配器视图<>适配器视图,视图观点,INT I,长L){
        RelativeLayout的vwParentRow =(RelativeLayout的)view.getParent();
        ArrayList的<串GT;接触=新的ArrayList<串GT;();
        最后复选框contactCheckBox =(复选框)vwParentRow.findViewById(R.id.sendCheckBox);
        TextView的contactTextView =(TextView中)vwParentRow.findViewById(R.id.contactNameTextView);
        Toast.makeText(PendingFriendsActivity.this,contactTextView.getText()的toString(),Toast.LENGTH_LONG。).show();        如果(contactCheckBox.isChecked())
        {
            contact.add(fullContacts.get(ⅰ)获得(0));
            contact.add(fullContacts.get(ⅰ)获得(1));
            sendContactList.add(接触);
        }其他{
            sendContactList.remove(ⅰ);
        }
    }    @覆盖
    公共无效onNothingSelected(适配器视图<>适配器视图){    }
});builderSingle.setCancelable(假);
builderSingle.setTitle(好友邀请);
builderSingle.setNegativeButton(取消,buttonClicked);
builderSingle.setPositiveButton(发送邀请,buttonClicked);
builderSingle.setAdapter(listAdapter,buttonClicked);
builderSingle.show();


解决方案

尝试是这样的:

  AlertDialog.Builder建设者=新AlertDialog.Builder(MainActivity.this);                //做一个清单举行各种颜色的状态
                的for(int i = 0; I< colors.length;我++){
                    ColorVO colorVO =新ColorVO();
                    colorVO.setName(颜色[I]);
                    colorVO.setSelected(checkedColors [I]);
                    colorList.add(colorVO);
                }                //做的东西在这里传递这个两个数组只的ArrayList('颜色'和;'checkedColors')
                builder.setMultiChoiceItems(颜色,checkedColors,新DialogInterface.OnMultiChoiceClickListener(){
                    @覆盖
                    公共无效的onClick(DialogInterface对话,诠释它,布尔器isChecked){
                        //设置状态列表中VO
                        colorList.get(所).setSelected(器isChecked);
                        Toast.makeText(getApplicationContext(),
                                colorList.get(所).getName()++器isChecked,Toast.LENGTH_SHORT).show();
                    }
                });                builder.setCancelable(假);                builder.setTitle(您的preferred颜色?);                builder.setPositiveButton(OK,新DialogInterface.OnClickListener(){
                    @覆盖
                    公共无效的onClick(DialogInterface对话,诠释它){
                        txtSelected.setText(您的preferred颜色..... \\ n);                        //保存选定VOS的状态
                        ArrayList的< ColorVO> selectedList =新的ArrayList<>();
                        的for(int i = 0; I< colorList.size();我++){
                            ColorVO colorVO = colorList.get(ⅰ);
                            颜色[I] = colorVO.getName();
                            checkedColors [I] = colorVO.isSelected();
                            如果(colorVO.isSelected()){
                                selectedList.add(colorVO);
                            }
                        }                        的for(int i = 0; I< selectedList.size();我++){
                            //如果元素是去年那么不重视逗号或将其附加
                            如果(ⅰ= selectedList.size() - 1)
                                txtSelected.setText(txtSelected.getText()+ selectedList.get(ⅰ).getName()+,);
                            其他
                                txtSelected.setText(txtSelected.getText()+ selectedList.get(ⅰ).getName());
                        }
                        colorList.clear();
                    }
                });                builder.setNegativeButton(否,新DialogInterface.OnClickListener(){
                    @覆盖
                    公共无效的onClick(DialogInterface对话,诠释它){
                        //确保清除列表中不重复这里形成
                        colorList.clear();
                    }
                });                builder.setNeutralButton(取消,新DialogInterface.OnClickListener(){
                    @覆盖
                    公共无效的onClick(DialogInterface对话,诠释它){
                        //确保清除列表中不重复这里形成
                        colorList.clear();
                    }
                });                AlertDialog对话框= builder.create();
                dialog.show();

希望这可以帮助!

My problem is that I am trying to open up a dialogInterface with a ListView that has a row with 1 TextView and a CheckBox. The problem I have is that when the checkBox is clicked, lets say on item 2, item 6 will also be checked, and item 10, and item 14, and all the way when all I want is item 2 to be picked.

What I have tried:

 //setup a list adapter and then set that on the list
ListAdapter listAdapter = new ArrayAdapter(PendingFriendsActivity.this, R.layout.activity_contact_list_item, R.id.contactNameTextView, ContactList);
AlertDialog.Builder builderSingle = new AlertDialog.Builder(PendingFriendsActivity.this);
DialogInterface.OnClickListener buttonClicked;
buttonClicked = new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which){
            case DialogInterface.BUTTON_POSITIVE:
                for(int i = 0; i < sendContactList.size();i++)
                {
                    String phone, name;
                    name = sendContactList.get(i).get(0);
                    phone = sendContactList.get(i).get(1);
                    sendSMS(phone);
                }
                break;

            case DialogInterface.BUTTON_NEGATIVE:
                //No button clicked
                break;
        }
    }
};
builderSingle.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        RelativeLayout vwParentRow = (RelativeLayout)view.getParent();
        ArrayList<String> contact = new ArrayList<String>();
        final CheckBox contactCheckBox = (CheckBox) vwParentRow.findViewById(R.id.sendCheckBox);
        TextView contactTextView = (TextView) vwParentRow.findViewById(R.id.contactNameTextView);
        Toast.makeText(PendingFriendsActivity.this, contactTextView.getText().toString(),Toast.LENGTH_LONG).show();

        if(contactCheckBox.isChecked())
        {
            contact.add(fullContacts.get(i).get(0));
            contact.add(fullContacts.get(i).get(1));
            sendContactList.add(contact);
        } else {
            sendContactList.remove(i);
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
});

builderSingle.setCancelable(false);
builderSingle.setTitle("Friend Invite");
builderSingle.setNegativeButton("Cancel", buttonClicked);
builderSingle.setPositiveButton("Send Invites", buttonClicked);
builderSingle.setAdapter(listAdapter, buttonClicked);
builderSingle.show();

解决方案

Try something like this :

 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

                // make a list to hold state of every color
                for (int i = 0; i < colors.length; i++) {
                    ColorVO colorVO = new ColorVO();
                    colorVO.setName(colors[i]);
                    colorVO.setSelected(checkedColors[i]);
                    colorList.add(colorVO);
                }

                // Do something here to pass only arraylist on this both arrays ('colors' & 'checkedColors')
                builder.setMultiChoiceItems(colors, checkedColors, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        // set state to vo in list
                        colorList.get(which).setSelected(isChecked);
                        Toast.makeText(getApplicationContext(),
                                colorList.get(which).getName() + " " + isChecked, Toast.LENGTH_SHORT).show();
                    }
                });

                builder.setCancelable(false);

                builder.setTitle("Your preferred colors?");

                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        txtSelected.setText("Your preferred colors..... \n");

                        // save state of selected vos
                        ArrayList<ColorVO> selectedList = new ArrayList<>();
                        for (int i = 0; i < colorList.size(); i++) {
                            ColorVO colorVO = colorList.get(i);
                            colors[i] = colorVO.getName();
                            checkedColors[i] = colorVO.isSelected();
                            if (colorVO.isSelected()) {
                                selectedList.add(colorVO);
                            }
                        }

                        for (int i = 0; i < selectedList.size(); i++) {
                            // if element is last then not attach comma or attach it
                            if (i != selectedList.size() - 1)
                                txtSelected.setText(txtSelected.getText() + selectedList.get(i).getName() + " ,");
                            else
                                txtSelected.setText(txtSelected.getText() + selectedList.get(i).getName());
                        }
                        colorList.clear();
                    }
                });

                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // make sure to clear list that duplication dont formed here
                        colorList.clear();
                    }
                });

                builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // make sure to clear list that duplication dont formed here
                        colorList.clear();
                    }
                });

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

Hope this can help.!

这篇关于用一个ListView DialogInterface的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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