通过 CursorAdapter 显示多个 YouTube 缩略图

Display multiple YouTube Thumbnail via CursorAdapter

我一直在为一个未捕获的错误而绞尽脑汁,我的 YouTube 缩略图适配器为零,它可能做错了,但我找不到修复它的地方。有人在 cursoradapter 上有可用的 youtubethumbnail 视图吗?

错误信息:

    java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
at com.google.android.youtube.player.internal.r.i(Unknown Source)
at com.google.android.youtube.player.internal.o.k(Unknown Source)
at com.google.android.youtube.player.internal.o.a(Unknown Source)
at com.google.android.youtube.player.internal.p.<init>(Unknown Source)
at com.google.android.youtube.player.internal.ac.a(Unknown Source)
at com.google.android.youtube.player.YouTubeThumbnailView$a.a(Unknown Source)
at com.google.android.youtube.player.internal.r.g(Unknown Source)
at com.google.android.youtube.player.internal.r$c.a(Unknown Source)
at com.google.android.youtube.player.internal.r$b.a(Unknown Source)
at com.google.android.youtube.player.internal.r$a.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)

代码如下:

public class YTAdapter extends CursorAdapter implements YouTubeThumbnailView.OnInitializedListener {

private String youtubeKey;
YouTubeThumbnailView youTubeThumbnailView;
YouTubeThumbnailLoader youTubeThumbnailLoader;
private final Map<YouTubeThumbnailView, YouTubeThumbnailLoader> thumbnailLoaderMap;

public YTAdapter(Context context, Cursor c, int flags) {
    super(context, c, flags);
    thumbnailLoaderMap = new HashMap<YouTubeThumbnailView, YouTubeThumbnailLoader>();
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.item_trailer, parent, false);
}

@Override
public void bindView(View view, final Context context, final Cursor cursor) {
    Log.d(LOG_TAG, "binding view: " + view.toString());

    TextView trailerNameTextView = (TextView) view.findViewById(R.id.trailer_name);

    String trailerName = cursor.getString(
            cursor.getColumnIndex(
                    YTContract
                            .YTEntry
                            .COLUMN_TITLE)
    );

    trailerNameTextView.setText(trailerName);

    youtubeKey = cursor.getString(
            cursor.getColumnIndex(
                    YTContract
                        .YTEntry
                        .COLUMN_YOUTUBE_KEY)
    );

    youTubeThumbnailView = new YouTubeThumbnailView(context);
    youTubeThumbnailView = (YouTubeThumbnailView) view.findViewById(R.id.youtube_thumbnail);

    youTubeThumbnailView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String youtubeKey = (String) v.getTag();
            final Intent lightboxIntent = new Intent(context, YoutubeLightBox.class);
            lightboxIntent.putExtra(YoutubeLightBox.KEY_VIDEO_ID, youtubeKey);
            context.startActivity(lightboxIntent);
        }
    });

    youTubeThumbnailView.setTag(youtubeKey);
    youTubeThumbnailView.initialize(BuildConfig.YOUTUBE_API_TOKEN, this);

}

public void release (){
    for (YouTubeThumbnailLoader loader : thumbnailLoaderMap.values()) {
        loader.release();
    }
}

@Override
public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader thumbnailLoader) {

    youTubeThumbnailLoader = thumbnailLoader;
    thumbnailLoader.setOnThumbnailLoadedListener(new ThumbnailListener());
    thumbnailLoaderMap.put(youTubeThumbnailView,thumbnailLoader);
    youTubeThumbnailLoader.setVideo(youTubeThumbnailView.getTag().toString());
}

private final class ThumbnailListener implements
        YouTubeThumbnailLoader.OnThumbnailLoadedListener {

    @Override
    public void onThumbnailLoaded(YouTubeThumbnailView thumbnail, String videoId) {
    }

    @Override
    public void onThumbnailError(YouTubeThumbnailView thumbnail,
                                 YouTubeThumbnailLoader.ErrorReason reason) {   }
}

@Override
public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult 

经过几天尝试解决该错误后,我发现通过缩略图视图库显示缩略图没有任何意义,因为它只是另一个图像视图。我完成了整个适配器 ,没有 youtubeThumbnailView API,只使用普通的旧 textview 和 imageview。而且效果很好。

@Override
public void bindView(View view, final Context context, final Cursor cursor) {
    TextView trailerNameTextView = (TextView) view.findViewById(R.id.trailer_name);
    ImageView imageView = (ImageView) view.findViewById(R.id.youtube_thumbnail);

    String trailerName = cursor.getString(
            cursor.getColumnIndex(
                    YTContract
                            .YTEntry
                            .COLUMN_TITLE)
    );

    trailerNameTextView.setText(trailerName);

    youtubeKey = cursor.getString(
            cursor.getColumnIndex(
                    YTContract
                        .YTEntry
                        .COLUMN_YOUTUBE_KEY)
    );


    String youtubeImageUrl = "http://img.youtube.com/vi/";

    Uri imageUri = Uri.parse(youtubeImageUrl).buildUpon()
            .appendPath(youtubeKey)
            .appendPath("default.jpg")
            .build();

    Picasso.with(context)
            .load(imageUri)
            .placeholder(R.drawable.placeholder)
            .error(R.drawable.error)
            .tag(context)
            .into(imageView);

}