1

タイトルが少し乱雑であることは知っていますが、ここに問題があります....私の目標は、タイトルを取得し、サムネイル URL を使用して YouTube チャンネルの動画のサムネイルを listView に描画することです...これまでのところ、私は動画タイトルをきちんと表示するためのtextViewですが、とにかくサムネイルが描画できませんでした.....ちなみに、json / sqliteのクラスは適切に行われており、データを適切に取得できるので、心配する必要はありません。 ..私を悩ませる唯一のことは、サムネイルが表示されないことです.imageViewはアプリの空のスペースとして表示されます....

これが私のコードです。手を貸してください。どうも

これは、アクティビティの on create メソッドです...

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    String[] uiBindFrom = { TutListDatabase.COL_TITLE, TutListDatabase.COL_THUMBNAIL };
    int[] uiBindTo = { R.id.title, R.id.thumbnail };

    getLoaderManager().initLoader(TUTORIAL_LIST_LOADER, null, this);

    adapter = new SimpleCursorAdapter(
            getActivity().getApplicationContext(), R.layout.list_item,
            null, uiBindFrom, uiBindTo,
            CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
    adapter.setViewBinder(new MyViewBinder());
    setListAdapter(adapter);
}

これは listView に何かを入れるためのプライベートクラスです...

private class MyViewBinder implements SimpleCursorAdapter.ViewBinder{

    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        int viewId = view.getId();
        switch(viewId){
        case R.id.title:
            TextView titleTV = (TextView)view;
            titleTV.setText(cursor.getString(columnIndex));
            break;

                    // it is not displaying any thumbnail in app....
        case R.id.thumbnail:
            ImageView thumb = (ImageView) view;
            thumb.setImageURI(Uri.parse(cursor.getString(columnIndex)));

        break;
        }
        return false;
    }

}

ここにxmlレイアウトファイルがあります...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/thumbnail"
    android:layout_width="101dp"
    android:layout_height="101dp"
    android:src="@drawable/icon" />

<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="6dp"
    android:textSize="24dp" />


</LinearLayout>
4

1 に答える 1

2

私が使用した方法をお見せできますが、それは非常にうまく機能しました。まず、画像をキャッシュする方法が必要であり、これまでに見た中で最良の方法は、優れた Google IO プレゼンテーションで説明されているように LruCache を使用することです。より少なく: http://www.youtube.com/watch?v=gbQb1PVjfqM

これが、そのプレゼンテーションで説明されている方法の私の実装です。

public class BitmapCache extends LruCache<String, Bitmap> { 

    public BitmapCache(int sizeInBytes) {
        super(sizeInBytes);
    }   

    public BitmapCache(Context context) {
        super(getOptimalCacheSizeInBytes(context));
    }   

    public static int getOptimalCacheSizeInBytes(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

        int memoryClassBytes = am.getMemoryClass() * 1024 * 1024;

        return memoryClassBytes / 8;
    }

    @Override
    protected int sizeOf(String key, Bitmap value) {
        return value.getRowBytes() * value.getHeight();
    }
}

次に、AsyncTask を使用して画像を非同期的に読み込む必要があります。次の実装では、指定された ImageView に画像を読み込み、キャッシュを処理します。

public class LoadImageAsyncTask extends AsyncTask<Void, Void, Pair<Bitmap, Exception>> {
    private ImageView mImageView;
    private String mUrl;
    private BitmapCache mCache;

    public LoadImageAsyncTask(BitmapCache cache, ImageView imageView, String url) {
        mCache = cache;
        mImageView = imageView;
        mUrl = url;

        mImageView.setTag(mUrl);
    }

    @Override
    protected void onPreExecute() {
        Bitmap bm = mCache.get(mUrl);

        if(bm != null) {
            cancel(false);

            mImageView.setImageBitmap(bm);
        }
    }

    @Override
    protected Pair<Bitmap, Exception> doInBackground(Void... arg0) {
        if(isCancelled()) {
            return null;
        }

        URL url;
        InputStream inStream = null;
        try {
            url = new URL(mUrl);
            URLConnection conn = url.openConnection();

            inStream = conn.getInputStream();

            Bitmap bitmap = BitmapFactory.decodeStream(inStream);

            return new Pair<Bitmap, Exception>(bitmap, null);

        } catch (Exception e) {
            return new Pair<Bitmap, Exception>(null, e);
        }
        finally {
            closeSilenty(inStream);
        }
    }

    @Override
    protected void onPostExecute(Pair<Bitmap, Exception> result) {
        if(result == null) {
            return;
        }

        if(result.first != null && mUrl.equals(mImageView.getTag())) {
            mCache.put(mUrl, result.first);
            mImageView.setImageBitmap(result.first);
        }
    }

    public void closeSilenty(Closeable closeable) {
        if(closeable != null) {
        try {
            closeable.close();
        } catch (Exception e) {
            // TODO: Log this
        }
        }
    }
}

次に、onCreate(...) または onActivityCreated(...) で、ListView をホストしている Activity または Fragment に BitmapCache のインスタンスを作成する必要があります。

mBitmapCache = new BitmapCache(this); // or getActivity() if your using a Fragment

ここで、画像を表示する SimpleCursorAdapter を更新する必要があります。私のプロジェクトに固有のコードとしてほとんどのコードを省略しましたが、値がカーソルにバインドされている値である必要がある setViewImage をオーバーライドするという考え方です。 imageview を null に設定して、アイテムに関連付けられたキャッシュからの奇数の画像がないことを確認します。

    @Override
    public void setViewImage(ImageView iv, String value) {
        final String url = value;
        iv.setImageBitmap(null);
        new LoadImageAsyncTask(mBitmapCache, iv, url).execute();

    }

アップデート

明確にするために、アダプターは次のようになります

    adapter = new SimpleCursorAdapter(
            context, R.layout.list_item,
            null, uiBindFrom, uiBindTo,
            CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER) {
        @Override
        public void setViewImage(ImageView iv, String value) {
                final String url = value;
                iv.setImageBitmap(null);
                new LoadImageAsyncTask(mBitmapCache, iv, url).execute();
        }
    };

それが役立つことを願っています!

于 2012-09-20T13:40:11.483 に答える