迄今爲止,我已經實現了一個有2個視頻的視頻視圖,我可以從數據庫(標題,上傳的用戶,觀看次數等)設置所有其他文字視圖,但我無法設置持續時間,因爲我只是從數據庫中獲取我的視頻網址後計算這一點,但是讓我的視頻時長後,2件事情之一發生:ListView自定義適配器 - 添加視頻持續時間
- 1)持續時間只有列表更新的最後一個TextView的項目
- 2)當我將setText移出get duration方法時,出現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))
當我使用其中任何一個,沒有工作。
但我做arrDuration.add(video_length);那麼爲什麼我的listview適配器沒有從arrDuration數組中獲取我的項目呢? – user3560827
@ user3560827,因爲我說onPrepared只會被最後一個視圖調用,並且在添加你的'arrDuration.size()= 1'之後,但是位置== n。檢查您的適配器只有一個項目,它將完全工作。我建議你使用HashMap來代替列表。那麼你的代碼就是:\ n'arrDuration.put(position,video_length); tvDuration.setText(arrDuration.get(位置));' –