如何使用带有MediaStore的Custom SimpleCursorAdapter获取ListView的正确图像?

时间:2013-11-15 08:38:00

标签: android listview simplecursoradapter mediastore

我现在有像youtube缩略图样式的项目, 我已经创建了自定义simplecursoradapter 使用mediastore显示图像,标题,艺术家,sdcard视频的持续时间,

我的问题是唯一的图片是错误但是标题,艺术家,持续时间已经有序,我注意到的是图片没有从视频中获得正确的缩略图, 标题,艺术家,持续时间是唯一没有问题的,

我在我的SD卡上有10个视频, 列表视图以正确的顺序显示标题,艺术家,持续时间 但图像是不是核心的东西,我拖动列表也改变了图像, 我想为每个列表订单获得正确的视频图像..

这是我的代码

public class TABThisWeek extends ListActivity {

    Cursor videoCursor;
    int videoColumnIndex;

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

        final Uri sourceUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        String[] projection = { MediaStore.Video.Media._ID,
                MediaStore.Video.Media.DATA, MediaStore.Video.Media.TITLE,
                MediaStore.Video.Media.DISPLAY_NAME,
                MediaStore.Video.Media.ARTIST, MediaStore.Video.Media.DURATION };

        String orderBy = MediaStore.Video.Media.TITLE;

        // CREATE CURSOR THAT WILL HOLD ALL VALUE
        videoCursor = getContentResolver().query(sourceUri, projection, null,
                null, orderBy);

        // THE DESIRED COLUMNS TO BE BOUND
        String[] from = { MediaStore.Video.Media.TITLE,
                MediaStore.Video.Media.ARTIST, MediaStore.Video.Media.DURATION};

        // THE XML DEFINED VIEWS WHICH THE DATA WILL BE BOUND TO
        int[] to = { R.id.list_Title, R.id.list_Artist, R.id.list_Duration };

        /*
         * CREATE THE ADAPTER USING THE CURSOR POINTING TO THE DESIRED DATA AS
         * WELL AS THE LAYOUT INFORMATION
         */
        MyCustomAdapter adapter = new MyCustomAdapter(this,
                R.layout.list_row_items, videoCursor, from, to);

        setListAdapter(adapter);

        setListViewAttributes(); // set listview divider color etc
    }

    /* get ListActivity's own ListView and sets the divider color */
    private void setListViewAttributes() {

        ListView lvTab = getListView();
        ColorDrawable cd = new ColorDrawable(this.getResources().getColor(
                R.color.color_divider_black));
        lvTab.setDivider(cd);
        lvTab.isScrollbarFadingEnabled();
        lvTab.setVerticalFadingEdgeEnabled(true);
        lvTab.setFadingEdgeLength(25);
        lvTab.setDividerHeight(1);
    }

    /* Custom Adapter for TabThisWeek */
    public class MyCustomAdapter extends SimpleCursorAdapter {

        private Cursor c;
        private int layout;
        private final LayoutInflater inflater;
        private MyViewHolder holder;
        BitmapFactory.Options options;

        public MyCustomAdapter(Context context, int layout, Cursor c,
                String[] from, int[] to) {
            super(context, layout, c, from, to);
            // this.context = context;
            this.c = c;
            this.layout = layout;
            this.inflater = LayoutInflater.from(context);
        }

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

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            super.bindView(view, context, cursor);

            holder = (MyViewHolder) view.getTag();

            if (holder == null) {
                holder = new MyViewHolder();

                holder.titleHolder = (TextView) view
                        .findViewById(R.id.list_Title);
                holder.artistHolder = (TextView) view
                        .findViewById(R.id.list_Artist);
                holder.durationHolder = (TextView) view
                        .findViewById(R.id.list_Duration);
                holder.imageHolder = (ImageView) view
                        .findViewById(R.id.list_Image);

                /* getting the index because auto loop */
                holder.titleIndex = cursor
                        .getColumnIndexOrThrow(MediaStore.Video.Media.TITLE);
                holder.artistIndex = cursor
                        .getColumnIndexOrThrow(MediaStore.Video.Media.ARTIST);
                holder.durationIndex = cursor
                        .getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
                holder.imageIndex = cursor.getInt(cursor
                        .getColumnIndexOrThrow(MediaStore.Video.Media._ID));

                view.setTag(holder);
            }

            /* set the Title but if null set to default from resources */
            try {
                holder.titleHolder.setText(cursor.getString(holder.titleIndex));
            } catch (Exception e) {
                holder.titleHolder.setText(getResources().getString(
                        R.string.default_text_title));
            }

            /* set the artist but if null set to default from resources */
            try {
                holder.artistHolder.setText(cursor
                        .getString(holder.artistIndex)); // temp
            } catch (Exception e) {
                holder.artistHolder.setText(getResources().getString(
                        R.string.default_text_artist));
            }

            /* set the time duration if null set to default */
            try {
                holder.durationHolder.setText(cursor
                        .getString(holder.durationIndex)); // temp
            } catch (Exception e) {
                holder.durationHolder.setText(getResources().getString(
                        R.string.default_text_duration));
            }

            try {
                options = new BitmapFactory.Options();
                options.inDither = false;
                options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                options.inPreferredConfig = Bitmap.Config.RGB_565;

                holder.bitmapVidThumb = MediaStore.Video.Thumbnails
                        .getThumbnail(context.getContentResolver(),
                                holder.imageIndex,
                                MediaStore.Video.Thumbnails.MICRO_KIND, options);

                holder.imageHolder.setImageBitmap(holder.bitmapVidThumb);
            } catch (Exception e) {
                holder.imageHolder.setBackgroundDrawable(getResources()
                        .getDrawable(R.drawable.default_img));
            }

            /* set default value if title is null */
            if (holder.titleHolder.getText().toString().equals("")) {
                holder.titleHolder.setText(getResources().getString(
                        R.string.default_text_title));
            }

            /* set default value if artist is null */
            if (holder.artistHolder.getText().toString().equals("")) {
                holder.artistHolder.setText(getResources().getString(
                        R.string.default_text_artist));
            }

            /* to get and check if time is below 0 for exception */
            holder.durationTemp = Long.parseLong(holder.durationHolder
                    .getText().toString());
            if (holder.durationTemp <= 0) {
                holder.durationHolder.setText(getResources().getString(
                        R.string.default_text_duration));
            }
               }

        /* my nested view holder class */
        class MyViewHolder {
            Bitmap bitmapVidThumb;

            ImageView imageHolder;
            TextView titleHolder;
            TextView artistHolder;
            TextView durationHolder;

            int imageIndex;
            int titleIndex;
            int artistIndex;
            int durationIndex;
            long durationTemp;

        }
    }

}

1 个答案:

答案 0 :(得分:1)

当适配器回收行时,您将获得属于另一行的holder.imageIndex的值。您应该将光标操作移到if(holder == null)部分之外,这样它们就不会从另一行中回收。

以下是编辑bindView函数的方法:

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);

        holder = (MyViewHolder) view.getTag();

        if (holder == null) {
            holder = new MyViewHolder();

            holder.titleHolder = (TextView) view
                    .findViewById(R.id.list_Title);
            holder.artistHolder = (TextView) view
                    .findViewById(R.id.list_Artist);
            holder.durationHolder = (TextView) view
                    .findViewById(R.id.list_Duration);
            holder.imageHolder = (ImageView) view
                    .findViewById(R.id.list_Image);

            view.setTag(holder);
        }

        /* getting the index because auto loop */
        holder.titleIndex = cursor
            .getColumnIndexOrThrow(MediaStore.Video.Media.TITLE);
        holder.artistIndex = cursor
            .getColumnIndexOrThrow(MediaStore.Video.Media.ARTIST);
        holder.durationIndex = cursor
            .getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
        holder.imageIndex = cursor.getInt(cursor
            .getColumnIndexOrThrow(MediaStore.Video.Media._ID));


        //... the rest of the bindView function