android 自定义baseadapter listview,Android 给listView设置自定义继承BaseAdapter的adapter。给item设置监听的问题...

博客围绕Android开发中自定义BaseAdapter展开,作者遇到在监听事件里访问外部类全局变量时,获取的值始终是data的第一个值,而在监听事件外获取正常的问题,还给出了相关代码示例,如CommentAdapter类的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为什么 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 data = new ArrayList();

CustomerComment customerComment;

private Context context;

private ImageLoad imageLoad;

public CommentAdapter() {

}

public CommentAdapter(Context context) {

this.context = context;

imageLoad = new ImageLoad(context);

}

public CommentAdapter(List 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;

}

}

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

相关代码

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值