2013-11-15 62 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 - 我已經訪問了該網站,但我無法找到解決我的問題的方法,但感謝您的信息! –

相關問題