1

我正在使用以下 LazyAdapter。我需要在点击事件中获取点击位置。我在第 20 行收到以下建议。有什么建议吗?

不能引用在不同方法中定义的内部类中的非最终变量位置

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

2 回答 2

6

像这样声明getView

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

从 aanonymous inner class你只能访问final局部变量。

发生这种情况是因为内部类的代码可能在初始化类的方法完成并且它的局部变量被垃圾收集之后被调用。声明变量final使变量成为常量,并将其作为值发送到内部类,而不是作为引用。

于 2012-06-13T11:30:14.923 回答
0

只需使用具有构造函数 -> MyOwnClickListener(位置)的属性位置实现您自己的 OnClickListener 类,并实现 onClick 的逻辑。

class MyOwnClickListener implements OnClickListener {
        int position;

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

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

并在 getView 方法中设置您的侦听器:

holder.click.setOnClickListener(new MyOwnClickListener(position) {
于 2014-03-16T17:47:05.817 回答