Ladessa
Ladessa

Reputation: 1003

Button setOnClickListener in Listview

I'm trying to change the background of a Button which is in a listview implementing onClickListener but I'm getting this error:

enter image description here

Here is the code:

        convertView = inflater.inflate(R.layout.item_quiz3, null);
                holder.textView1 = (TextView) convertView
                        .findViewById(R.id.textView1);
                holder.textViewSim = (TextView) convertView
                        .findViewById(R.id.Sim);
                holder.textViewNao = (TextView) convertView
                        .findViewById(R.id.Nao);
                holder.btnSim = (ImageButton) convertView
                        .findViewById(R.id.btnSim);
                holder.btnNao = (ImageButton) convertView
                        .findViewById(R.id.btnNao);

                holder.btnNao.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                          v.setBackground(R.drawable.rdb_on);

                    }
                };

                }
  return convertView; }
}

Upvotes: 0

Views: 174

Answers (3)

Gaurav Gupta
Gaurav Gupta

Reputation: 4691

The compile time error indicates that you are using Dialog's onclicklistener, but your button is a view not a dialog. Hence you need to explicitly tell the compiler that you need View's OnClickListener.

holder.btnNao.setOnClickListener(new View.OnClickListener() {
          @Override
                public void onClick(View v) {
  }
}

Upvotes: 0

Sethu
Sethu

Reputation: 428

Try this one. i think your are missing in close branthesis.')'

      holder.btnNao.setOnClickListener(new ListenerT() (or) OnClickListener()
            (or) new View.OnClickListener(){

                    @Override
                    public void onClick(View v) {
                          v.setBackground(R.drawable.rdb_on);

                    }
                });



private class ListenerT implements OnClickListener {

                 public ListenerT() {
        }

        @Override
        public void onClick(View v) {

        }
    }

Upvotes: 0

Tsunaze
Tsunaze

Reputation: 3254

You have the wrong importation.

Use :

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

                @Override
                public void onClick(View v) {
                      v.setBackground(R.drawable.rdb_on);

                }
            };

Or delete the DialogInterface import

Upvotes: 2

Related Questions