Marco Faion
Marco Faion

Reputation: 607

Android - OnClickListener issue

i'm tryng to implement an OnClickListener on some buttons that are repeated in a GridView, but Eclipse give me an error on the btn.setOnClickListener Line.

This is the error:

1 method(s) to implement:
- android.view.View.OnClickListener.onClick()

And This is the code:

package com.example.convert;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;

public class convert extends Activity {
    //private ListView List;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String [] elenco = {
                "ciao","questo","è","un esempio","Configurazione"
                };

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.oggetto,R.id.testogg,elenco);
        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(arrayAdapter);
        Button btn = (Button) (findViewById(R.id.testogg));

        btn.setOnClickListener(new Button.OnClickListener(){

              OnClickListener(View v) {
                  setContentView(R.layout.oggetto);
              }

            });
    }
}

I don't understand what that mean, i'm new to java.

Thanks!

Upvotes: 1

Views: 6084

Answers (4)

Matt
Matt

Reputation: 5684

When you implement an onClickListener, you need to Override the OnClick() Method. This will change what happens when something is actually clicked.

It's giving you the error message because onClickListener is an Interface and one of the stipulations of implementing an interface is that you must implement all of the methods defined in the interface. More information on interfaces can be found at wiki: http://en.wikipedia.org/wiki/Interface_(Java)

also, your method should take a view as a parameter and look something like:

public void onClick(View v){
 //Handle Click
}

Upvotes: 1

Falmarri
Falmarri

Reputation: 48577

Remove the Button class from your onClickListener declaration.

    btn.setOnClickListener(new OnClickListener(){
          @Override
          public void onClick(View v) {
              setContentView(R.layout.oggetto);
          }

        });

OnClickListener is a property of a View.

Upvotes: 3

Andrei Fierbinteanu
Andrei Fierbinteanu

Reputation: 7826

What your doing inside the setOnClickListener seems to be creating an anonymous class (don't know the android api). That means that Button.OnClickListener is an interface or abstract class, that defines some unimplemented methods, and you are defining a new class that implements that interface, so you must define an implementation for that method. In this case it seems that there's an onClick() method that you must define (makes sense since it's an on click listener, you need to tell it what to do when a click event occurs).

So you need to do something like:

btn.setOnClickListener(new Button.OnClickListener(){

   OnClickListener(View v) {
       setContentView(R.layout.oggetto);
   }

   public void onClick() {
       // what to do on a click event
   }

});

Upvotes: 1

dsr
dsr

Reputation: 1316

Please update your code with :

       btn.setOnClickListener(new OnClickListener(){

         public void onClick(View v) {
              setContentView(R.layout.oggetto);
          }

        });

Upvotes: 0

Related Questions