gpt4 book ai didi

android - 获取选中复选框的位置

转载 作者:行者123 更新时间:2023-11-29 01:54:35 25 4
gpt4 key购买 nike

关于获取所选文件的位置,我遇到了一个奇怪的问题。它最多只能获取前 6 个文件。如果我选择第 7 个文件,则它不会返回任何内容。如果我只点击第 7 个文件。它会将我作为第一个文件返回。

编辑:我认为问题是由于滚动时 View 发生变化,并且复选框被重置。

这是我的部分代码。

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

不确定哪里出了问题。

最佳答案

如果您使用的是 ViewHolder 模式,那么您需要了解列表中的每个项目都没有 ViewHolder。对于 Android 为您回收的每个 View,您只有一个 ViewHolder。如果您可以在显示屏上放置 6 个项目,那么您可能会有 8 或 9 个 ViewHolder。这意味着当 getView() 被调用时,您需要更新 ViewHolder 中与正在显示的项目相关的所有内容。在您的情况下,这意味着您需要移动此代码:

    viewHolder.checkbox.setId(position);
viewHolder.checkbox.setTag(list.get(position));

if block 之外并在所有情况 中执行此操作(无论您刚刚创建了一个 ViewHolder 还是只是回收了一个)。我会这样做:

    } else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.checkbox.setTag(list.get(position));
viewHolder.checkbox.setId(position);

另外,您应该将您的 ViewHolder 类声明为 static,因为它不需要对周围类的引用。

关于android - 获取选中复选框的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15931618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com