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();
     }
 }
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论