如何从数组列表中获取子类对象?

时间:2015-12-01 20:34:52

标签: java android

我无法在' onPostExecute'中获取数组列表的早期类对象。我的异步课程的方法。为了简化这篇文章的内容,我只想设置' name'我的onPostExecute方法中的textview就像这样。

  

myTextView.setText(VideoDetail_List.get(0).getVideoName());

但问题是返回的String是字符串数组中videoname对象的最后一个实例,而不是视频名称的第一个实例,它应该是0索引处VideoDetail_List的子类对象

  

AsyncTask Class

        final JSONObject obj = new JSONObject(result);
        final JSONArray videoData = obj.getJSONArray("data");

        final int n = videoData.length();

        for (int i = 0; i < n; ++i) {
            final JSONObject video = videoData.getJSONObject(i);
            videoDetail.setVideoName(video.getString("name"));
            videoDetail.setVideoDuration(video.getString("duration"));
            final JSONObject videoPictures = video.getJSONObject("pictures");
            final JSONArray videoPictureSizes = videoPictures.getJSONArray("sizes");
            final JSONObject videoPictureLink = videoPictureSizes.getJSONObject(1);

            videoDetail.setPictureLink(videoPictureLink.getString("link"));
            //pictureLink.add(videoPictureLink.getString("link"));

            final JSONArray videoURLTypes = video.getJSONArray("download");
            final JSONObject videoURLLink = videoURLTypes.getJSONObject(0);

            videoDetail.setVideoLink(videoURLLink.getString("link"));
            //videoLink.add(videoURLLink.getString("link"));
            VideoDetail_List.add(i,videoDetail);

        }

        return VideoDetail_List;

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}


@Override
protected void onPostExecute(List<VideoDetail> VideoDetail_List) {


    System.out.println("RESPONSE: " + VideoDetail_List);

    final TextView myTextView = new TextView(mContext);

    myTextView.setText(VideoDetail_List.get(0).getVideoName());



    mLayout.addView(myTextView);

    myTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String sText = myTextView.getText().toString();
            //Toast toast = Toast.makeText(this, sText, Toast.LENGTH_SHORT);
            Toast toast = Toast.makeText(mContext, sText, Toast.LENGTH_LONG);
            toast.show();



        }
    });


}
  

VideoDetail Class

public class VideoDetail {
    private String videoName;
    private String videoDuration;
    private String pictureLink;
    private String videoLink;




    public String getVideoName() {
        return this.videoName;
    }

    public void setVideoName(String videoName) {
        this.videoName = videoName;
    }

    public String getVideoDuration() {
        return videoDuration;
    }

    public void setVideoDuration(String videoDuration) {
        this.videoDuration = videoDuration;
    }

    public String getPictureLink() {
        return pictureLink;
    }

    public void setPictureLink(String pictureLink) {
        this.pictureLink = pictureLink;
    }

    public void setVideoLink(String videoLink){
        this.videoLink = videoLink;
    }
    public String getVideoLink() {
        return videoLink;
    }


}

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

  

然而问题是返回的String是最后一个   字符串数组中的videoname对象的实例

那是因为你在for循环的每次迭代中都覆盖了VideoDetails唯一实例的内容。您应该在每次迭代时实例化它

 for (int i = 0; i < n; ++i) {
     videoDetail = new VideoDetail();