账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Android 给listView设置自定义继承BaseAdapter的adapter。给item设置监听的问题
    • 2020-01-01 00:00
    • 11
    40
    0

    为什么 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
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • 野心〃与梦想 普通会员 1楼

      在Android中,如果你需要给一个列表视图设置自定义的Adapter,你可以使用BaseAdapter。BaseAdapter是Android提供的一个简单实现列表数据源的类,它提供了适配器接口,你可以根据你的需要创建一个自定义的Adapter。

      然后,你可以给列表视图设置一个自定义的Adapter,然后在Adapter中为每个item设置监听器。具体的代码如下:

      ```java // 创建一个自定义的Adapter BaseAdapter adapter = new CustomAdapter();

      // 将自定义的Adapter添加到listView中 listView.setAdapter(adapter); ```

      在CustomAdapter中,你可以定义你的数据源接口,然后创建你的自定义的数据结构。然后,你可以为每个item设置监听器。例如:

      ```java // 定义你的数据源接口 public interface CustomAdapterDataSource { void insert(int position, CustomItem item); void delete(int position); void update(int position, CustomItem item); CustomItem getItem(int position); }

      // 创建一个自定义的数据源 CustomAdapterDataSource dataSource = new CustomAdapterDataSource() { @Override public void insert(int position, CustomItem item) { // 在这里插入你的数据 }

      @Override
      public void delete(int position) {
          // 在这里删除你的数据
      }
      
      @Override
      public void update(int position, CustomItem item) {
          // 在这里更新你的数据
      }
      
      @Override
      public CustomItem getItem(int position) {
          // 在这里获取你的数据
      }
      

      };

      // 创建一个自定义的Adapter CustomAdapter adapter = new CustomAdapter(dataSource); ```

      这样,你就可以在listView中为每个item设置监听器了。例如,你可以在Adapter中为每个item添加一个方法,用于处理listView的点击事件。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部