Edward Vaghela
Edward Vaghela

Reputation: 19

setOnItemClickListener error in ListView wth ArrayAdapter

i'm developing android app, which requires listview with ArrayAdapter. now i want onItemClickListener on this listview. but the IDE, Anacode(same as eclipse), shows an error for it!

I have tried every answers in posts of questions like this. but no help!

Please help

//the code 
package edward.harsh.friends;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;   

public class ListViewSampleActivity extends Activity
{
    ListView mCountriesLV;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /**
         * Get the control instances from the main.xml layout
         */
        mCountriesLV = (ListView) findViewById(R.id.countriesLV);
        mCountriesLV.setAdapter(new CountriesAdapter(this, R.layout.listview_country_row, mCountriesList));



        mCountriesLV.setOnItemClickListener(
          new OnItemClickListener() 
          {
                   @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext()," name", Toast.LENGTH_SHORT).show();
}
           });

   }
}

error message is

   The method    setOnItemClickListener(AdapterView.OnItemClickListener in the type
  AdapterView <ListAdapter> is not applicable for the arguments (new   OnItemClickListener(){})

OnItemClickListener cannot be resolved to a type

as suggested by some. I even tried this

  mCountriesLV.setOnItemClickListener(new AdapterView.OnItemClickListener() 
          {
                   @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext()," name", Toast.LENGTH_SHORT).show();
}
           });

but it throws another error

The type new AdapterView.OnItemClickListener(){} must implement the inherited abstract method
AdapterView.OnItemClickListener.onItemClick(AdapterView <?>, View, int, long)

Upvotes: 0

Views: 436

Answers (2)

Mohit Suthar
Mohit Suthar

Reputation: 9375

Try to add AdapterView.OnItemClickListener() in setOnItemClickListener method, its not applicable for only OnItemClickListener

mCountriesLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    });

Upvotes: 1

siddhesh
siddhesh

Reputation: 563

use this code instead

mCountriesLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {}

Your Listener should be AdapterView.OnItemClickListener

Upvotes: 0

Related Questions