2

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

不知道哪里出错了。

4

1 回答 1

1

如果您正在使用该ViewHolder模式,那么您需要了解您的列表中的每个项目都没有一个ViewHolder。对于Android 正在为您回收的ViewHolder每个s,您只有一个。View如果您可以在显示器上放置 6 个项目,那么您可能会有 8 或 9ViewHolder秒。这意味着当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,因为它不需要对周围类的引用。

于 2013-04-10T17:28:20.993 回答