猿问

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

这是模型类


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 抛出以下错误。


哆啦的时光机
浏览 145回答 2
2回答

慕码人2483693

您需要使用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;    }

郎朗坤

用这个替换你的 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; }希望能帮到你
随时随地看视频慕课网APP

相关分类

Java
我要回答