2017-06-14 10 views
0

これまで2つのビデオでビデオビューを実装しましたが、データベース(タイトル、アップロードしたユーザー、ビューの数など)から他のすべてのテキストビューを設定できましたが、私はしかし、私のビデオ再生時間を取得した後、データベースから自分の動画のURLを取得した後、これを計算していますので、期間、2つのことの一つが起こる:ListViewカスタムアダプター - ビデオの長さを追加

  • 1)期間は、唯一のリストの最後のTextViewの項目を更新し
  • 2)getTextメソッドからsetTextを移動すると、IndexOutOfBoundExceptionが発生します。

以下の私のコード:

  @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      // TODO Auto-generated method stub 

      ViewHolder_video holder; 

      View row=convertView; 
      if(row==null) 
      { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = inflater.inflate(R.layout.activity_video_single, parent, false);  
      } 

      holder = new ViewHolder_video();     
      holder.ibProfilePicture = (ImageButton) row.findViewById(R.id.ibProfilePicture); 

      TextView tvID = (TextView) row.findViewById(R.id.tvID); 
      VideoView vv = (VideoView) row.findViewById(R.id.vv); 
      TextView tvTitle = (TextView) row.findViewById(R.id.tvTitle); 
      tvDuration = (TextView) row.findViewById(R.id.tvDuration); 
      TextView tvUser = (TextView) row.findViewById(R.id.tvUser); 
      TextView tvViews = (TextView) row.findViewById(R.id.tvViews); 
      tvDate = (TextView) row.findViewById(R.id.tvDate); 
      ImageButton ibProfilePicture = (ImageButton) row.findViewById(R.id.ibProfilePicture); 

      tvID.setText(arrID.get(position)); 

      //VideoView URL 
      vv.setVideoURI(Uri.parse(arrVideoURL.get(position))); 
      vv.seekTo(5000); 
      vv.pause(); 

      //VideoView Duration 
      vv.setOnPreparedListener(new OnPreparedListener() { 

       @Override 
       public void onPrepared(MediaPlayer mp) { 

        //get video duration 
        int duration=mp.getDuration()/1000; 
        int hours = duration/3600; 
        int minutes = (duration/60) - (hours * 60); 
        int seconds = duration - (hours * 3600) - (minutes * 60) ; 

        video_length = String.format("%d:%02d:%02d", hours, minutes, seconds); 

        if (hours == 0) 
        { 
         video_length = String.format("%02d:%02d", minutes, seconds); 
        } 

        System.out.println("0--> video_length: "+video_length + " "+position); // I get my TWO video durations as position 0 and position 1 
        arrDuration.add(video_length); // I add my durations to an array (List<String> arrDuration) 

        tvDuration.setText(arrDuration.get(position)); // <-- setting the text here only sets the duration to the last item only 
       } 
      }); 


      //tvDuration.setText(arrDuration.get(position)); // <-- setting the text here returns error: IndexOutOgBoundException 

      tvTitle.setText(arrTitle.get(position)); 
      tvUser.setText(arrUser.get(position)); 
      tvViews.setText(arrViews.get(position)); 
      tvDate.setText(arrDate.get(position)); 

      Picasso.with(context).load(arrProfilePicture.get(position)).resize(250, 250) 
      .transform(new CircleTransform()).placeholder(R.drawable.ic_launcher).error(R.drawable.error).into(ibProfilePicture); 

      holder.ibProfilePicture.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        //Toast.makeText(context, "go to user profile: "+arrUser.get(position), Toast.LENGTH_SHORT).show(); 
       } 
      }); 

      return row; 
     } 

私はそのうちの一つ、いずれも作業のいずれかを使用するときに2 tvDuration.setText(arrDuration.get(position))に注意してください。

答えて

0

あなたが使用VideoViewある場合、それは、MediaPlayerを呼ぶあなたのコレクションarrDuration.add(video_length)それをする時間を追加するときonPrepareコールバックが最後の項目(position == lastIndex)と を取得するので、あなたのコードでは、それは、あなたがIndexOutOfBoundExceptionを持っている理由VideoViewとアダプタの最後の項目を提供します最初の位置に追加されます。

チェックこのチュートリアルステップバイステップhttps://medium.com/@v.danylo/implementing-video-playback-in-a-scrolled-list-listview-recyclerview-d04bc2148429

+0

が、私はarrDuration.add(video_lengthを)やっています。なぜ私のリストビューアダプタはarrDuration配列からアイテムを取得しないのですか? – user3560827

+0

@ user3560827私はonPreparedが最後のビューによってのみ呼び出され、 'arrDuration.size()= 1'を追加した後に位置== nを追加したと言っていたからです。 1つの項目だけを使ってアダプタをチェックすると、正確に動作します。 List の代わりにHashMap を使用することをお勧めします。あなたのコードは次のようになります:\ n 'arrDuration.put(position、video_length); tvDuration.setText(arrDuration.get(position)); ' –

関連する問題