简体   繁体   English

需要帮助以获取listitem在onClickListener中的位置

[英]Need help to get the position of listitem inside onClickListener

I am using the following LazyAdapter. 我正在使用以下LazyAdapter。 I need to get the clicked position inside the click event. 我需要在click事件中获取被点击的位置。 I got the following suggestion at line 20. Any suggestions? 我在第20行收到以下建议。

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;
    }
}

Declare getView like this: 像这样声明getView

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. anonymous inner class您只能访问final局部变量。

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. 声明变量final将使该变量成为常量,并将其作为值(而不是引用)发送到内部类。

Just implement your own OnClickListener class with property position that has constructor -> MyOwnClickListener (position) and implement the logic for onClick. 只需使用具有构造函数-> MyOwnClickListener(位置)的属性position来实现自己的OnClickListener类,并实现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 : 并在getView方法中设置您的侦听器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM