2013-11-15 106 views
0

我有项目,现在像YouTube的缩略图风格, 我已经创建自定义simplecursoradapter 从SD卡使用mediastore显示图像,标题,艺术家,视频的时长,如何使用MediaStore自定义SimpleCursorAdapter为ListView获取正确的图像?

我的问题是只有图像错误但标题,艺术家,持续时间已经是为了,我注意到的是图像没有得到从视频的权利缩略图, 标题,艺术家,持续时间是唯一没有问题,

我有在我的SD卡10视频, 列表视图显示标题,艺术家,持续时间以正确的顺序 但成像e是财产以后不corect和ii拖动列表中的图像变化也, 我想也得到每列表顺序视频右图像..

这是我的代码

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; 

     } 
    } 

} 
+0

不要使用'SimpleCursorAdapter'.You应该使用'SimpleCursorAdapter'只有通过当你不需要任何定制。使用'BaseAdapter'查看http://gypsynight.wordpress.com/2012/02/17/how-to-show-all-video-file-stored-in-your-sd-card-in-a-listview/ – GrIsHu

+0

,因为如果想使用持有人我需要定制它?因为如果我不使用自定义简单的光标适配器我的列表视图性能会减慢,我的问题是如何从我的SDCARD中的所有视频使用MediaStore为我的imageview的列表视图获取正确的图像,如何做到这一点? –

回答

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 
+0

我可以建议您访问Vogella.com。那里有一个教程,完全涵盖了tjav所提出的观点。 http://www.vogella.com/articles/AndroidListView/article.html – Theo

+0

@ jtav - 所有持有人的操作应该把它放在(holder == null)之外?你可以编辑我自定义的简单光标适配器到你说的正确的那个,可以吗?非常感谢! –

+0

@ Theo - 我已经访问了该网站,但我无法找到解决我的问题的方法,但感谢您的信息! –

相关问题