简体   繁体   中英

OnItemClickListener Cannot instantiate the type AdapterView.OnItemClickListener

I have OnItemClick attacked to a listview and within the onItemClick() it opens a dialog with radio buttons, Cancel and Select. Within the dialog, I guess i need some sort of clickListener or itemSelectedListener so I can pass the value of the listview item they selected.

@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();
                }
            }
        });

But I get an error saying Cannot instantiate the type AdapterView.OnItemClickListener. What is the best route or how can I fix this so I can let the code know which radio button the user selected? Thanks!

If I understand you correctly, you want a listener to use when a RadioButton is checked . For this use, onCheckedChangeListener

You can use the second parameter to get the id of the RadioButton that was checked then you can do whatever you need with it from there.

yourRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) 
    { 
        if (checkedId == R.id.some_id)
        {
            // do something
        }

    });

AlertDialog使用setItems()方法 内置了对列表的支持 -没有理由制作自己的自定义ListView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM