Manikandan
Manikandan

Reputation: 1519

Need help to get the position of listitem inside onClickListener

I am using the following LazyAdapter. I need to get the clicked position inside the click event. I got the following suggestion at line 20. Any suggestions?

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

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private List<String> data;
    private static LayoutInflater inflater = null;
    public ImageLoader imageLoader;
ArrayList<String> text= new ArrayList<String>();
    public LazyAdapter(MainActivity a, List<String> parsed_string) {
        // TODO Auto-generated constructor stub
        activity = a;
        data = parsed_string;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolder {
        public TextView text;
        public ImageView image;
        public Button click;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        ViewHolder holder;


        if (convertView == null) {
            vi = inflater.inflate(R.layout.item, null);
            holder = new ViewHolder();
            holder.text = (TextView) vi.findViewById(R.id.text);
            ;
            holder.image = (ImageView) vi.findViewById(R.id.image);
            holder.click = (Button) vi.findViewById(R.id.btn_click);
            holder.click.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(activity, "clicked the button in the row",
                            Toast.LENGTH_LONG).show();
                    holder.click.setText(text.get(position)); // line 20
                }
            });
            vi.setTag(holder);
        } else
            holder = (ViewHolder) vi.getTag();

        holder.text.setText("item " + position);
        holder.image.setTag(data.get(position));

        imageLoader.DisplayImage(data.get(position), activity, holder.image);
        return vi;
    }
}

Upvotes: 1

Views: 1010

Answers (2)

makkasi
makkasi

Reputation: 7288

Just implement your own OnClickListener class with property position that has constructor -> MyOwnClickListener (position) and implement the logic for onClick.

class MyOwnClickListener implements OnClickListener {
        int position;

        public MyOwnClickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
....

and set your listener in getView method :

holder.click.setOnClickListener(new MyOwnClickListener(position) {

Upvotes: 0

tibtof
tibtof

Reputation: 7957

Declare getView like this:

public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    //...
}

From a anonymous inner class you can only access final local variables.

This happens because the code from the inner class may be called after the method that initializes the class finished and it's local variables garbage collected. Declaring the variable final makes the variable a constant and it's send to the inner class as value, not as reference.

Upvotes: 6

Related Questions