3

here's the code, I have problem with:

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

            if(listview.isItemChecked(position)){

            } 

            if(listview.getCheckedItemCount()>1){

            }

        }
    });

it keeps saying that listener must override superclass method and that I cant call non-final variable "listview" inside inner class. how am I supposed to call listview.isItemChecked(position) then? thanks

Miloš Lukačka
  • 842
  • 1
  • 11
  • 25
  • 1
    Check and see the project's compliance(right click project->Properties->Java compiler->Compiler compliance->make it 1.6). In java you can't use a variable in a anonymous inner class unless it's made `final`. You either make the `listview` variable final or you use the `parent` parameter which is actually your `ListView` for which you set the listener. – user Aug 22 '12 at 08:06
  • http://stackoverflow.com/a/5997998/726863 – Lalit Poptani Aug 22 '12 at 08:08
  • I checked that, I have 1.7. and thanks, I've fixed problem with that listview. – Miloš Lukačka Aug 22 '12 at 08:14
  • still the same problem.. – Miloš Lukačka Aug 22 '12 at 08:40
  • oh ... I'm so stupid, I didnt check project->properties->java compiler, but window->properties->java compiler... thanks, it works now – Miloš Lukačka Aug 22 '12 at 08:54

1 Answers1

3

I'd suggest you read up on the use of final in Java. Technically, you could fix this by adding the keyword final before ListView listview = ..., so final ListView listview = ....

However, a better option is to just use the passed reference to ListView in your OnItemClick-method. The parameter AdapterView<?> Parent corresponds to your ListView, so you can use the following code:

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

        ListView lv = (ListView) parent;

        if(lv.isItemChecked(position)){
            // ...
        } 

        if(lv.getCheckedItemCount()>1){
            // ...
        }

    }
});
Reinier
  • 3,836
  • 1
  • 27
  • 28