我有一个关于获取所选文件位置的奇怪问题。它最多只能获取前 6 个文件。如果我在第 7 个文件上选择,则它不会返回任何内容。如果我只是点击第 7 个文件。它会将我作为第一个文件返回。
编辑:我认为问题是由于我滚动时视图发生了变化,并且复选框被重置。
这是我的代码的一部分。
private boolean[] selection;
private int count;
private List<Media> getMediaList = new ArrayList<Media>();
private ArrayList<String> storeSelectedMedia = new ArrayList<String>();
String[] projection = {
MediaStore.Video.Media._ID,
MediaStore.Video.Media.ARTIST,
MediaStore.Video.Media.TITLE,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.DURATION,
MediaStore.Video.Media.SIZE
};
cursor = getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
projection,
null,
null,
null);
while(cursor.moveToNext()){
getMediaList.add(new Media (cursor.getString(2), cursor.getString(6), cursor.getString(3), cursor.getString(5)));
//Log.i("media", "" + cursor.getString(5));
}
cursor.close ();
count = getMediaList.size();
Log.i("MediaSize", "" + count);
selection = new boolean[count];
ArrayAdapter<Media> adapter = new MyMediaAdapter(this, getMediaList);
setListAdapter(adapter);
public class MyMediaAdapter extends ArrayAdapter<Media> {
private final List<Media> list;
private final Activity context;
public MyMediaAdapter(Activity context, List<Media> list) {
super(context, R.layout.media_view, list);
this.context = context;
this.list = list;
}
private class ViewHolder {
protected TextView fName, fSub, fDuration, fSize;
protected CheckBox checkbox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.media_view, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.fName = (TextView) view.findViewById(R.id.tvfname);
viewHolder.fSub = (TextView) view.findViewById(R.id.tvsub);
viewHolder.fDuration = (TextView) view.findViewById(R.id.tvduration);
viewHolder.fSize = (TextView) view.findViewById(R.id.tvsize);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkbox.setId(position);
viewHolder.checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) v;
int id = cb.getId();
if (selection[id]) {
cb.setChecked(false);
selection[id] = false;
} else {
cb.setChecked(true);
selection[id] = true;
}
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.fName.setText(list.get(position).getName());
holder.fSub.setText(list.get(position).getPath());
// Converting duration from String to Long
long milli = Long.valueOf(list.get(position).getDuration());
// Put it in % min, % sec format to display
holder.fDuration.setText(String.format("%d:%d sec",
TimeUnit.MILLISECONDS.toMinutes(milli),
TimeUnit.MILLISECONDS.toSeconds(milli) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milli))
));
// Convert data size from String to Long
long datasize = Long.valueOf(list.get(position).getData());
// Put in human readable format
holder.fSize.setText(readableFileSize(datasize));
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
final ArrayList<Integer> posSel = new ArrayList<Integer>();
posSel.clear();
storeSelectedMedia.clear();
/*
* Construct the list of selected items
*/
boolean noSelect = false;
Log.i("MediaSelection", "" + selection.length);
for (int i = 0; i < selection.length; i++) {
if (selection[i] == true) {
noSelect = true;
Log.e("Mediasel pos thu-->", "" + i);
posSel.add(i);
storeSelectedMedia.add(getMediaList.get(i).getPath());
}
}
switch (item.getItemId()) {
case R.id.action_refresh:
try {
rescanSdcard();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Toast.makeText(this, "Action refresh selected", Toast.LENGTH_SHORT)
.show();
finish();
startActivity(getIntent());
break;
case R.id.action_findfile:
if (noSelect) {
if (storeSelectedMedia.size() > 0) {
for (String file : storeSelectedMedia) {
try {
// Doing something
Log.i("Media", file.toString());
} catch (Exception e) {
Toast.makeText(this,
"Exception: '" + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
}
我与 Log.i(“媒体”)相关的日志我选择了 7 个文件。但它只在日志中显示 6。
04-11 00:31:26.879: I/MediaSelection(23155): 22
04-11 00:31:26.879: E/Mediasel pos thu-->(23155): 0
04-11 00:31:26.879: E/Mediasel pos thu-->(23155): 1
04-11 00:31:26.879: E/Mediasel pos thu-->(23155): 2
04-11 00:31:26.879: E/Mediasel pos thu-->(23155): 3
04-11 00:31:26.879: E/Mediasel pos thu-->(23155): 4
04-11 00:31:26.879: E/Mediasel pos thu-->(23155): 5
04-11 00:31:26.889: I/Media(23155): /storage/sdcard0/DCIM/100MEDIA/VIDEO0001.3gp
04-11 00:31:26.889: I/Media(23155): /storage/sdcard0/DCIM/100MEDIA/VIDEO0002.3gp
04-11 00:31:26.889: I/Media(23155): /storage/sdcard0/DCIM/100MEDIA/VIDEO0004.mp4
04-11 00:31:26.889: I/Media(23155): /storage/sdcard0/DCIM/100MEDIA/VIDEO0001.mp4
04-11 00:31:26.889: I/Media(23155): /storage/sdcard0/DCIM/100MEDIA/VIDEO0002.mp4
04-11 00:31:26.889: I/Media(23155): /storage/sdcard0/DCIM/100MEDIA/VIDEO0003.mp4
04-11 00:31:27.059: I/MediaSize(23155): 22
04-11 00:31:52.243: I/MediaSelection(23155): 22
04-11 00:31:52.243: E/Mediasel pos thu-->(23155): 0
04-11 00:31:52.243: I/Media(23155): /storage/sdcard0/DCIM/100MEDIA/VIDEO0001.3gp
04-11 00:31:52.353: I/MediaSize(23155): 22
不知道哪里出错了。