自定义数组适配器到视频片段到ListView

时间:2016-10-01 19:20:36

标签: android listview android-fragments android-videoview

我尝试制作自定义数组适配器以在列表视图中显示视频。据我所知,适配器旨在通过一个数组并在预定义的布局中显示它的内容。我有一个标准的字符串数组适配器,但似乎我的自定义适配器甚至没有显示。

这是我的代码:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.list_fragment, container, false);
        Bundle args = getArguments(); 
        int[] vids = args.getIntArray("vids"); //array of video resources
        //will convert items from aray into a list
        ListView story = (ListView) v;
        ListAdapter vid_view = new ListItemAdapter(getActivity(),foods);
        story.setAdapter(vid_view); //seems not to be loading anything

        return v;
    }

    public static list_page newInstance(int[] vids) {
        list_page page = new list_page();
        Bundle args = new Bundle();
        args.putIntArray("vids", vids);
        page.setArguments(args);

        return page;
    }

我的自定义适配器

private int[]vid_array;

    public ListItemAdapter(Context context, int[] vids) {
        super(context, R.layout.video_fragment,vids);
        this.vid_array = vids; //sets the array to video resource array
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup parent){
        LayoutInflater vid_inflater = LayoutInflater.from(getContext());
        View custom = vid_inflater.inflate(R.layout.video_fragment, parent, false);

        VideoView videoView = (VideoView) custom.findViewById(R.id.vid_view);
        String pack = getContext().getApplicationContext().getPackageName();
        Uri video_res = Uri.parse("android.resource://" + pack + "/" + vid_array[pos]);
        System.out.println("Video Resource: "+ vid_array[pos]); //I don't even see this in the console log
        videoView.setVideoURI(video_res);
        videoView.start();
        TextView vidText = (TextView) custom.findViewById(R.id.textView3);
        vidText.setText(vid_array[pos]);

        return custom;
    }

我是android的新手,我不知道我在哪里弄乱这个适配器。我应该如何将视频资源加载到一个数组中,由适配器解析并放入列表?

0 个答案:

没有答案