为了账号安全,请及时绑定邮箱和手机立即绑定

模型类在第二次使用时未将特定字符串传递给 ListViewAdapter

模型类在第二次使用时未将特定字符串传递给 ListViewAdapter

哆啦的时光机 2021-09-12 19:49:45
这是模型类public class Video {        private String videoTitle;        private String videoThumb;        private String videoChannel;        private String videoID;        // Constructor to convert JSON object into a Java class instance        public Video(JSONObject object) {            try {                JSONObject obj = object.getJSONObject("snippet");                this.videoTitle = obj.getString("title");                JSONObject obj3 = obj.getJSONObject("thumbnails").getJSONObject("maxres");                this.videoThumb = obj3.getString("url");                this.videoChannel = obj.getString("channelTitle");    //This is the Variable Which is not getting passed in other fragment                this.videoID = "eHarS-r_CC4";            } catch (JSONException e) {                e.printStackTrace();            }        }        public String getVideoTitle() { return videoTitle; }        public String getVideoThumb() {            return videoThumb;        }        public String getVideoChannel() {return videoChannel; }        public String getVideoID() {return videoID; }    }这是适配器类:    @Override    public View getView(int position, View convertView, ViewGroup parent) {        View view = convertView;        final ViewHolderVideo holder;        if (view == null) {            LayoutInflater inflater = LayoutInflater.from(context);            view = inflater.inflate(resource, parent, false);        Log.d("videoTitle", video.getVideoTitle());        final String videoID = video.getVideoID();        Log.d("videoID", videoID);}不是当我在 HomeFragment 中使用 Model 类和 Adapter 时,它为所有列表项同时传递 videoTitle 和 videoID,但是当我在其他片段中使用相同的模型类和 Adapter 时,它传递了 videoTitle,但会为 videoID 抛出以下错误。
查看完整描述

2 回答

?
慕码人2483693

TA贡献1860条经验 获得超9个赞

您需要使用BaseAdapter 类的setTag和getTag方法。


@Override

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


        ViewHolder holder;


        if ((convertView == null) || (convertView.getTag() == null)) {

            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = mInflater.inflate(R.layout.YOUR_LIST_ITEM, null);

            holder = new ViewHolder();

        } else {

            holder = (ViewHolder) convertView.getTag();

        }


        convertView.setTag(holder);



          /**

            * SET THE VALUES OF THE LISTVIEW'S ITEMS HERE 

           */



        return convertView;

    }


查看完整回答
反对 回复 2021-09-12
?
郎朗坤

TA贡献1921条经验 获得超9个赞

用这个替换你的 getter 方法。我已经修改了它


 public String getVideoTitle() { return this.videoTitle; }

    public String getVideoThumb() {

        return this.videoThumb;

    }

    public String getVideoChannel() {return this.videoChannel; }

    public String getVideoID() {return this.videoID; }

希望能帮到你


查看完整回答
反对 回复 2021-09-12
  • 2 回答
  • 0 关注
  • 144 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信