為什麼 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;
}
}
問題出現的環境背景及自己嘗試過哪些方法
相關代碼
// 請把代碼文本粘貼到下方(請勿用圖檔代替代碼)
你期待的結果是什麼?實際看到的錯誤資訊又是什麼?