通过CursorAdapter显示多个YouTube缩略图

时间:2016-02-11 04:25:40

标签: android-cursoradapter android-youtube-api

我一直在解决未被捕获的错误,并且我没有使用我的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 

1 个答案:

答案 0 :(得分:0)

在尝试解决错误几天之后,我发现通过thumbnailview库显示缩略图是没有意义的,因为它只是另一个imageview。我完成了整个适配器没有 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);

}