The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new DialogInterface.OnClickListener(){})

与世无争的帅哥 提交于 2019-12-01 08:17:58

问题


Attempting to add an onClickListener to items in my listView and I'm getting an error stating: "The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new DialogInterface.OnClickListener(){})" on the line:

 holder.imageView.setOnClickListener(new OnClickListener() {

The author of this article mentioned the following:

In your custom adapter class, you can try this code inside getView() method
[java]holder.imageView.setOnClickListener(new OnClickListener() {   
@Override
public void onClick(View v) {
Toast.makeText(context, "Clicked on image", Toast.LENGTH_LONG).show();
}
});[/java]

It seems as if I might need to modify my current implementation - I'm just not sure exactly how.

Source:

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        LayoutInflater inflater = context.getLayoutInflater();
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item2, null);
            holder = new ViewHolder();
            holder.txtSuccess = (TextView) convertView
                    .findViewById(R.id.success);
            holder.txtCmd = (TextView) convertView.findViewById(R.id.cmd);
            holder.txtPrice = (TextView) convertView.findViewById(R.id.price);
            holder.imageView = (ImageView) convertView
                    .findViewById(R.id.thumbnail);
            convertView.setTag(holder);
            holder.imageView.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    Toast.makeText(context, "Clicked on image",
                            Toast.LENGTH_LONG).show();
                }

回答1:


you probably have the wrong import. Check if you imported DialogInterface.OnClickListener. Still you can explicitly force the parameter this way:

holder.imageView.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
         Toast.makeText(context, "Clicked on image",
                Toast.LENGTH_LONG).show();
   }



回答2:


use View.OnClickListener instead of OnClickListener :

holder.imageView.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    Toast.makeText(context, "Clicked on image",
                            Toast.LENGTH_LONG).show();
                }

and use CTL+SHIFT+O to fix all imports



来源:https://stackoverflow.com/questions/21632011/the-method-setonclicklistenerview-onclicklistener-in-the-type-view-is-not-appl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!