为什么 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;
}
}
问题出现的环境背景及自己尝试过哪些方法
相关代码
// 请把代码文本粘贴到下方(请勿用图片代替代码)