在我的应用中,我在ListView中显示SD卡视频。 ListView显示视频缩略图,标题,大小,持续时间。当我向下滚动它显示相同的视频和缩略图但如果我点击一个项目它会显示另一个活动的不同视频文件。我只是希望在ListView中,实际视频将显示我是向下滚动还是向上滚动。
这是我的代码。
公共类LoadVideo扩展了AppCompatActivity {
private Cursor videoCursor;
private int video_column_index;
ListView videoList;
int count;
String[] thumbColumns = {MediaStore.Video.Thumbnails.DATA,
MediaStore.Video.Thumbnails.VIDEO_ID};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.load_video_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
init_phone_video_grid();
}
private void init_phone_video_grid()
{
String[] proj = {MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE,
MediaStore.Video.Media.DURATION};
videoCursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
count = videoCursor.getCount();
videoList = (ListView) findViewById(R.id.videoList);
videoList.setAdapter(new VideoAdapter(this.getApplicationContext()));
videoList.setOnItemClickListener(videoGridListener);
}
private AdapterView.OnItemClickListener videoGridListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
video_column_index = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
videoCursor.moveToPosition(i);
String fileName = videoCursor.getString(video_column_index);
Intent intent = new Intent(LoadVideo.this, ShowVideo.class);
intent.putExtra("videofilename", fileName);
startActivity(intent);
}
};
public class VideoAdapter extends BaseAdapter
{
Context context;
VideoAdapter(Context context)
{
this.context = context;
}
@Override
public int getCount() {
return count;
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup)
{
ViewHolder viewHolder;
String id;
if (view == null)
{
viewHolder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.list_items, viewGroup, false);
viewHolder.titleName = (TextView) view.findViewById(R.id.title);
viewHolder.duration = (TextView) view.findViewById(R.id.duration);
viewHolder.size = (TextView) view.findViewById(R.id.size);
viewHolder.thumbImage = (ImageView) view.findViewById(R.id.imageIcon);
//Title
video_column_index = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
videoCursor.moveToPosition(i);
id = videoCursor.getString(video_column_index);
viewHolder.titleName.setText(id);
//Size
video_column_index = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
videoCursor.moveToPosition(i);
String vSize;
long videoSize = videoCursor.getLong(video_column_index);
double actualSizeInMB = videoSize/(1024.0 * 1024.0);
DecimalFormat dec = new DecimalFormat("0.00");
if (actualSizeInMB > 1)
{
vSize = dec.format(actualSizeInMB).concat(" MB");
viewHolder.size.setText(vSize);
}
else
{
vSize = dec.format(actualSizeInMB).concat(" KB");
viewHolder.size.setText(vSize);
}
//Duration
video_column_index = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
videoCursor.moveToPosition(i);
long time = videoCursor.getLong(video_column_index);
long seconds = time/1000;
long minutes = seconds/60;
seconds = seconds % 60;
String setTime ;
if (seconds < 10)
{
setTime = String.valueOf(minutes) + ":0" + String.valueOf(seconds);
viewHolder.duration.setText(setTime.concat( " | "));
}
else
{
setTime = String.valueOf(minutes) + ":" + String.valueOf(seconds);
viewHolder.duration.setText(setTime.concat( " | "));
}
//String[] proj = { MediaStore.Video.Media._ID, MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.DATA };
//Cursor cursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj,
//MediaStore.Video.Media.DISPLAY_NAME + "=?", new String[] { id }, null);
//cursor.moveToFirst();
//long ids = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media._ID));
long ids = videoCursor.getInt(videoCursor.getColumnIndex(MediaStore.Video.Media._ID));
ContentResolver crThumb = getContentResolver();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, ids, MediaStore.Video.Thumbnails.MICRO_KIND, options);
viewHolder.thumbImage.setImageBitmap(curThumb);
curThumb = null;
//video_column_index = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
//videoCursor.moveToPosition(i);
//String path = videoCursor.getString(video_column_index);
//Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Video.Thumbnails.MICRO_KIND);
//viewHolder.thumbImage.setImageBitmap(thumbnail);
//videoThumbnailCursor.close();
}
return view;
}
private class ViewHolder
{
TextView titleName;
TextView size;
TextView duration;
ImageView thumbImage;
}
}
}
答案 0 :(得分:0)
从这一点开始:if(view == null)
,修改你的代码,如:
if (view == null) {
viewHolder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.list_items, viewGroup, false);
viewHolder.titleName = (TextView) view.findViewById(R.id.title);
viewHolder.duration = (TextView) view.findViewById(R.id.duration);
viewHolder.size = (TextView) view.findViewById(R.id.size);
viewHolder.thumbImage = (ImageView) view.findViewById(R.id.imageIcon);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)(view.getTag());
}
//Title.....and so on...