我正在使用以下 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;
}
}