开发者

Problem with onListItemClick and clicklisteners

I am trying to create a onListItemClick which will show to the user a dialog with two options. Each开发者_C百科 option should call a function to execute a certain action. The error i am getting is:

Cannot refer to a non-final variable position inside an inner class defined in a different method

 protected void onListItemClick(ListView l, View v, int position, long id){
 final CharSequence[] items = {"Delete", "Show"};

 MyPOI mpoi= myAdapter.getItem(position);

 AlertDialog.Builder builder = new AlertDialog.Builder(this);
 builder.setTitle("Pick an option");
 builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
           if(items[item].equals("Delete")){
               dbc.deletePOI(position);
           }
        }
    });


 AlertDialog alert = builder.create();

 alert.show();
 super.onListItemClick(l, v, position, id);
 }


Try making position final:

protected void onListItemClick(ListView l, View v, final int position, long id) {

and similarly for any other variables the compiler complains about.


Cannot refer to a non-final variable position inside an inner class defined in a different method

If instead of defining the class within a method, you define it (declare it) at the parent class level, you should have your problems solved. (Free tip ahead:) I usually prefer to avoid using inner classes because not having them will produce less coupled code making code-reuse easier.

Anyway, back to your problem, you should have something like

public class A {
      void method b(){
           something.setOnClickListener( new OnClickListener() { ... } );  // this is the inner class
      }
}

I suggest you to have something like

class A {
      void method b(){
           something.setOnClickListener( new BetterInnerClass());  // this is the inner class
      }

      private class BetterInnerClass implements OnClickListener{
           ...
      }
}


I actually manage to fix it myself. Here is my solution in case someone else has the same issue:

     MyPOI mpoi;
 protected void onListItemClick(ListView l, View v, int position, long id){
 final CharSequence[] items = {"Delete", "Show"};

  mpoi= myAdapter.getItem(position);

 AlertDialog.Builder builder = new AlertDialog.Builder(this);
 builder.setTitle("Select an option");
 builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
           if(items[item].equals("Delete")){
             executeDelete();
           }
        }
    });


 AlertDialog alert = builder.create();

 alert.show();
 super.onListItemClick(l, v, position, id);
 }
 public void executeDelete(){
     try{
int i=dbc.deletePOI(mpoi.poiId);
if(i==-2){          
    Toast.makeText(this, "Error in operation. Please try again", Toast.LENGTH_SHORT).show();    
}
else if(i==1){

    Toast.makeText(this, "Record deleted succesfully.", Toast.LENGTH_LONG).show();  


}

finish();
     }catch(Exception ex){           
         ex.printStackTrace();
     }

 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜