让ListView刷新? YouTube API - 自定义适配器

时间:2015-05-08 00:15:48

标签: android listview android-listview youtube

我试图利用YouTube API,所以我改变了他们的演示并做了我自己的小应用程序。这一切都很好,直到我解决了这个问题。

我根本无法让ListView刷新......我在StackOverflow上环顾四周,尝试了各种随机解决方案无济于事(

我更新列表的方法(是的,我传递了新对象)[注意,适配器是下面的PageAdapter类的一个实例]:

public static void forceListUpdate(){
    List<VideoEntry> list = new ArrayList<VideoEntry>();
    for(VideoEntry entry : VideoQueue.getQueue()){
        list.add(entry);
    }
    System.out.println("video_list is " + VIDEO_LIST);
    adapter.refresh(list);
    System.out.println("Data set changed!");
}

我正在使用他们的自定义PageAdapter类(使用我添加的刷新方法):

    private static class PageAdapter extends BaseAdapter {

    private final List<VideoEntry> entries;
    private final List<View> entryViews;
    private final Map<YouTubeThumbnailView, YouTubeThumbnailLoader> thumbnailViewToLoaderMap;
    private final LayoutInflater inflater;
    private final ThumbnailListener thumbnailListener;

    private boolean labelsVisible;

    public PageAdapter(Context context, List<VideoEntry> entries) {
        this.entries = entries;

        entryViews = new ArrayList<View>();
        thumbnailViewToLoaderMap = new HashMap<YouTubeThumbnailView, YouTubeThumbnailLoader>();
        inflater = LayoutInflater.from(context);
        thumbnailListener = new ThumbnailListener();

        labelsVisible = true;
    }
    public void refresh(List<VideoEntry> items)
    {
        VideoListFragment.VIDEO_LIST = items;
        notifyDataSetChanged();
    }
    public void releaseLoaders() {
        for (YouTubeThumbnailLoader loader : thumbnailViewToLoaderMap.values()) {
            loader.release();
        }
    }

    public void setLabelVisibility(boolean visible) {
        labelsVisible = visible;
        for (View view : entryViews) {
            view.findViewById(R.id.text).setVisibility(visible ? View.VISIBLE : View.GONE);
        }
    }

    @Override
    public int getCount() {
        return entries.size();
    }

    @Override
    public VideoEntry getItem(int position) {
        return entries.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        VideoEntry entry = entries.get(position);
        if (view == null) {
            view = inflater.inflate(R.layout.list_item, parent, false);
            YouTubeThumbnailView thumbnail = (YouTubeThumbnailView) view.findViewById(R.id.thumbnail);
            thumbnail.setTag(entry.videoId);
            thumbnail.initialize(DeveloperKey.DEVELOPER_KEY, thumbnailListener);
        } else {
            YouTubeThumbnailView thumbnail = (YouTubeThumbnailView) view.findViewById(R.id.thumbnail);
            YouTubeThumbnailLoader loader = thumbnailViewToLoaderMap.get(thumbnail);
            if (loader == null) {
                thumbnail.setTag(entry.videoId);
            } else {
                thumbnail.setImageResource(R.drawable.loading_thumbnail);
                loader.setVideo(entry.videoId);
            }
        }
        TextView label = ((TextView) view.findViewById(R.id.text));
        label.setText(entry.text);
        label.setVisibility(labelsVisible ? View.VISIBLE : View.GONE);
        return view;
    }

虽然可能不需要,但我的ListView XML:         

<LinearLayout
    android:id="@+id/video_box"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:orientation="vertical">

    <ImageButton
        android:id="@+id/close_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:src="@android:drawable/btn_dialog"
        android:onClick="onClickClose"/>

    <fragment
        class="com.my.project.ListActivity$VideoFragment"
        android:id="@+id/video_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

ListFragment类,其中调用setAdapter ...

    public static class VideoListFragment extends ListFragment {

    public static List<VideoEntry> VIDEO_LIST;
    static {
        List<VideoEntry> list = new ArrayList<VideoEntry>();
        for(VideoEntry entry : VideoQueue.getQueue()){
            list.add(entry);
        }
        VIDEO_LIST = list;
    }


    public static PageAdapter adapter;
    public static View videoBox;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new PageAdapter(getActivity(), VIDEO_LIST);
    }
    public static void forceListUpdate(){
        List<VideoEntry> list = new ArrayList<VideoEntry>();
        for(VideoEntry entry : VideoQueue.getQueue()){
            list.add(entry);
        }
        System.out.println("video_list is " + VIDEO_LIST);
        adapter.refresh(list);

        System.out.println("Data set changed!");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        videoBox = getActivity().findViewById(R.id.video_box);
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        setListAdapter(adapter);
    }
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        String videoId = VIDEO_LIST.get(position).videoId;
        FullscreenActivity.setVideoId(videoId);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        adapter.releaseLoaders();
    }

    public void setLabelVisibility(boolean visible) {
        adapter.setLabelVisibility(visible);
    }

}

所有引用但未提及的类都正常工作。 XML中的包名称已更改。我承认我可能没有在正确的括号使用等方面很好地粘贴课程,但我向你保证没有语法错误:)感谢您提供任何帮助!

1 个答案:

答案 0 :(得分:0)

看起来问题是您没有更新原始数据源VIDEO_LIST

我建议您更新原始数据源,然后在适配器上调用notifyDataSetChanged()Documentation....

public static void forceListUpdate(){
    //List<VideoEntry> list = new ArrayList<VideoEntry>(); //Don't create a new list here
    VIDEO_LIST.clear(); //clear out the old contents
    for(VideoEntry entry : VideoQueue.getQueue()){
        //list.add(entry);
        VIDEO_LIST.add(entry); //add new values to original data source for adapter
    }
    System.out.println("video_list is " + VIDEO_LIST);
    //adapter.refresh(list);
    adapter.notifyDataSetChanged(); //call this instead

    System.out.println("Data set changed!");
}