关于获取所选文件的位置,我有一个奇怪的问题。
它最多只能获取前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


不知道哪里出了问题。

最佳答案

如果使用的是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,因为它不需要引用周围的类。

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

10-13 02:26