Android 给listView设置自定义继承BaseAdapter的adapter。给item设置监听的问题

为什么 final String u_id = customerComment.getU_id();
String t_id = customerComment.getU_id();

   同样是customerComment.getU_id();获取出来的,输出来的值是不一样的。在onClickListen里面的t_id始终是data.get(0).getU_id()?而在onClickListen外面的是正常的。
        也就是在监听事件里访问外部类的全局变量customerComment时,始终是data的第一个 customerComment。
public class CommentAdapter extends BaseAdapter {

    List<CustomerComment> data = new ArrayList<CustomerComment>();
    CustomerComment customerComment;
    private Context context;
    private ImageLoad imageLoad;

    public CommentAdapter() {
    }

    public CommentAdapter(Context context) {
        this.context = context;
        imageLoad = new ImageLoad(context);
    }


    public CommentAdapter(List<CustomerComment> data, Context context) {
        this.data = data;
        this.context = context;
        imageLoad = new ImageLoad(context);
    }

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

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = View.inflate(context, R.layout.community_comment_item, null);
        }
        customerComment = data.get(position);
                //这里!!!!!!!!
       ** final String u_id = customerComment.getU_id();**
        ImageView headpic = convertView.findViewById(R.id.headpic);
        TextView name = convertView.findViewById(R.id.name);
        TextView publishtime = convertView.findViewById(R.id.publishtime);
        TextView comment = convertView.findViewById(R.id.comment);
        name.setText(customerComment.getName());
        publishtime.setText(customerComment.getTime());
        comment.setText(customerComment.getContent());

        //头像点击事件,跳到用户的首页
        headpic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(context, UserHomePage.class);
                                //这里!!!!!!!!
                String t_id = customerComment.getU_id();
                System.out.println(t_id+","+u_id);
                intent.putExtra("u_id",u_id);
                context.startActivity(intent);
            }
        });
        imageLoad.loadImage(headpic,context.getString(R.string.server_projectpath)+customerComment.getHeadPicPath());
        return convertView;
    }
}

问题出现的环境背景及自己尝试过哪些方法

相关代码

// 请把代码文本粘贴到下方(请勿用图片代替代码)

你期待的结果是什么?实际看到的错误信息又是什么?

阅读 1.5k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题