OnItemClickListener无法实例化类型AdapterView.OnItemClickListener

时间:2013-07-11 16:06:46

标签: android android-listview android-alertdialog

我将OnItemClick攻击到listview,并在onItemClick()内打开一个带有单选按钮的对话框,取消和选择。在对话框中,我想我需要某种clickListener或itemSelectedListener,所以我可以传递他们选择的listview项的值。

@Override
            public void onItemClick(AdapterView<?> parent, View view, int position,  long id) {
                if(position == 0) { 
                    CheckBox checkBox = (CheckBox) mRoot.findViewById(R.id.cbRowCheckBox);
                    checkBox.setChecked(!checkBox.isChecked());
                }
                if(position == 1) {

                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                    builder.setTitle(R.string.sync_frequency);

                    ListView listView = new ListView(getActivity());
                    listView.setOnItemClickListener(new OnItemClickListener());  <-----
                    String[] syncOptions = mSyncOptions;
                    ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.select_dialog_singlechoice, android.R.id.text1, syncOptions);
                    builder.setSingleChoiceItems(modeAdapter, 0, null);
                    builder.setView(listView);
                    builder.setPositiveButton(R.string.sync_select,null);
                    builder.setNegativeButton(R.string.sync_cancel,null);
                    final Dialog dialog = builder.create();
                    dialog.show();
                }
            }
        });

但是我得到一个错误,说无法实例化类型AdapterView.OnItemClickListener。什么是最佳路线或如何解决这个问题,以便让代码知道用户选择了哪个单选按钮?谢谢!

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您需要在listenerRadioButton时使用checked。对于此用途,onCheckedChangeListener

您可以使用第二个参数获取已检查的id的{​​{1}},然后您可以从中执行任何操作。

RadioButton

答案 1 :(得分:0)

AlertDialog built in support for lists使用setItems() method - 没有理由自行制作ListView