1

我正在尝试CheckBox. 但是当我点击复选框然后我滚动我的列表时,我看到在下一个项目列表中有一些复选框被选中。

这是图像

在此处输入图像描述

这是代码:

package com.tsdv.wds.GUI.Server;

import java.io.File;
import java.util.ArrayList;

import com.tsdv.wds.R;
import com.tsdv.wds.manager.FileItems;
import com.tsdv.wds.manager.FileManager;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ItemFileAdapter2 extends BaseAdapter implements OnClickListener{

    private static final String TAG = "ItemFileAdapter";
    Context context;
    FileManager fileManager;
    ArrayList<FileItems> arrayList;
    LayoutInflater inflater;
    ViewHolder holder;
    int mPosition;

    public ItemFileAdapter2(Context context, ArrayList<FileItems> arrayList, FileManager fileManager) {
        super();
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.fileManager = fileManager;
        this.arrayList = arrayList;
        Log.d(TAG, "create " + this.arrayList.size());
        //holder = new ViewHolder();

    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        mPosition = position;
        holder = new ViewHolder();
        if (null == convertView){
            convertView = inflater.inflate(R.layout.item_custom_lv_serverfile, null);
            convertView.setTag(holder);
            Log.d(TAG, "Position: " + position);
        } else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.llFilenameAndInfo = (LinearLayout) convertView.findViewById(R.id.llFilenameAndInfo);
        holder.llFilenameAndInfo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                File path = new File(arrayList.get(position).getParent()
                        .toString()
                        + File.separator
                        + arrayList.get(position).getName().toString());
                ServerMainActivity.setPath(path);
                arrayList = fileManager.displayDirectory(
                        arrayList.get(position).getParent(),
                        arrayList.get(position).getName());
                ItemFileAdapter2.this.notifyDataSetChanged();

            }
        });
        holder.imvThumbnail = (ImageView) convertView.findViewById(R.id.imvThumbnail);
        holder.imvThumbnail.setImageBitmap(arrayList.get(position).getThumnail());
        holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
        holder.tvName.setText(arrayList.get(position).getName());
        holder.tvInfo = (TextView) convertView.findViewById(R.id.tvInfo);
        holder.tvInfo.setText("Info");
        holder.cbRead = (CheckBox) convertView.findViewById(R.id.cbRead);
        holder.cbRead.setVisibility(View.VISIBLE);
        holder.cbWrite = (CheckBox) convertView.findViewById(R.id.cbWrite);
        holder.cbWrite.setVisibility(View.VISIBLE);
        if (!arrayList.get(position).isDirectory()){
            Log.d(TAG, "invisible: " + position);
            holder.cbRead.setVisibility(View.INVISIBLE);
            holder.cbWrite.setVisibility(View.INVISIBLE);
        }

        return convertView;
    }
    private class ViewHolder {
        LinearLayout llFilenameAndInfo;
        TextView tvName;
        TextView tvInfo;
        ImageView imvThumbnail;
        CheckBox cbRead;
        CheckBox cbWrite;
    }
    @Override
    public void onClick(View v) {
        if (arrayList.get(mPosition).isDirectory()) {
            File path = new File(arrayList.get(mPosition).getParent().toString() + File.separator + arrayList.get(mPosition).getName().toString());
            ServerMainActivity.setPath(path);
            arrayList = fileManager.displayDirectory(arrayList.get(mPosition).getParent(), arrayList.get(mPosition).getName());
            ItemFileAdapter2.this.notifyDataSetChanged();
        }

    }
}
4

2 回答 2

0

当您使用 convertView(它是一个正在重用/回收的视图)时,convertView 中已经存在一个检查,因为之前已经检查过。

使用此代码将解决您的问题。

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    mPosition = position;
    holder = new ViewHolder();
    if (null == convertView){
        convertView = inflater.inflate(R.layout.item_custom_lv_serverfile, null);
        convertView.setTag(holder);
        Log.d(TAG, "Position: " + position);
    } else{
        holder = (ViewHolder) convertView.getTag();
    }

    holder.llFilenameAndInfo = (LinearLayout) convertView.findViewById(R.id.llFilenameAndInfo);
    holder.llFilenameAndInfo.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            File path = new File(arrayList.get(position).getParent()
                    .toString()
                    + File.separator
                    + arrayList.get(position).getName().toString());
            ServerMainActivity.setPath(path);
            arrayList = fileManager.displayDirectory(
                    arrayList.get(position).getParent(),
                    arrayList.get(position).getName());
            ItemFileAdapter2.this.notifyDataSetChanged();

        }
    });
    holder.imvThumbnail = (ImageView) convertView.findViewById(R.id.imvThumbnail);
    holder.imvThumbnail.setImageBitmap(arrayList.get(position).getThumnail());
    holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
    holder.tvName.setText(arrayList.get(position).getName());
    holder.tvInfo = (TextView) convertView.findViewById(R.id.tvInfo);
    holder.tvInfo.setText("Info");
    holder.cbRead = (CheckBox) convertView.findViewById(R.id.cbRead);
    holder.cbRead.setVisibility(View.VISIBLE);
    holder.cbRead.setChecked(false);
    holder.cbWrite = (CheckBox) convertView.findViewById(R.id.cbWrite);
    holder.cbWrite.setVisibility(View.VISIBLE);
    holder.cbWrite.setChecked(false);
    if (!arrayList.get(position).isDirectory()){
        Log.d(TAG, "invisible: " + position);
        holder.cbRead.setVisibility(View.INVISIBLE);
        holder.cbWrite.setVisibility(View.INVISIBLE);
    }


    // check it based on value in List.

    return convertView;
}

要存储检查的值,请添加一个 CheckChangedListener 并将检查状态存储在布尔列表中并在 getView 上检索它。

于 2012-08-17T04:36:24.053 回答
0

请注意,当您向上/向下滚动时,ListAdapter 概念正在重用隐藏的视图。这使得另一个 Item View 与之前隐藏的 Item View 具有相同的状态。

尝试将保存 cbRead 和 cbWrite 值的布尔类变量添加到您的 FileItems 类,并在 getView 内部根据“位置”参数获取 FileItems 对象,在返回 convertView 对象之前,设置 cbRead 和 cbWrite 对象的值。

让我知道这是否解决了您的问题。

于 2012-08-17T04:15:44.153 回答