自定义列表中的复选框

时间:2012-08-17 03:44:32

标签: android listview custom-adapter

我正在尝试CheckBox的求解器事件。但当我点击复选框后,我滚动我的列表,我看到在下一个项目列表中有一些复选框已被选中。

这是图片

enter image description here

以下是代码:

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();
        }

    }
}

2 个答案:

答案 0 :(得分:0)

请注意,当您向上/向下滚动时,ListAdapter概念会重复使用隐藏的视图。这使得其他项目视图与之前隐藏的项目视图具有相同的状态。

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

如果这解决了您的问题,请告诉我。

答案 1 :(得分: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;
}

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