我正试图愚弄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类(带有我添加的refresh方法):
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中的以上软件包名称已更改。我承认我可能在正确使用括号等方面没有很好地粘贴该类,但是我向您保证没有语法错误:)感谢您提供的任何帮助!
最佳答案
看来问题在于您没有更新原始数据源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!");
}
关于android - 获取一个ListView刷新? YouTube API-自定义适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30113747/