我有一个与获取所选文件位置有关的奇怪问题。 它只能达到前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
不确定哪里出错了。
答案 0 :(得分:1)
如果您使用的是ViewHolder
模式,那么您需要了解列表中的每个项目都没有ViewHolder
。您为Android回收的每个ViewHolder
只有一个View
。如果你可以在显示器上放置6个项目,那么你可能会有8或9 ViewHolder
个。这意味着当调用getView()
时,您需要更新ViewHolder
中与所显示项目相关的所有内容。在您的情况下,这意味着您需要移动此代码:
viewHolder.checkbox.setId(position);
viewHolder.checkbox.setTag(list.get(position));
在if
区块中,并在所有情况中执行此操作(无论您是刚刚创建了ViewHolder
还是只是回收了一个)。我会这样做:
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.checkbox.setTag(list.get(position));
viewHolder.checkbox.setId(position);
另外,您应该将ViewHolder
课程声明为static
,因为它不需要引用周围的课程。