android中的自定义复选框难度 [英] custom checkbox difficulty in android

查看:14
本文介绍了android中的自定义复选框难度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何修改这个imageCheckBoxAdapter代码以在滚动时保持checkBox的状态(即所有被选中的复选框即使在滚动后也应该保持选中状态.同时被选中的变量需要存储在一个数组中)?

how to modify this imageCheckBoxAdapter code in order to maintain status of checkBox when scrolled(that is all the checkboxes which are checked should remain checked even after scrolling. Also the checked variables need to be stored in an array)?

class imageCheckBoxAdapter extends ArrayAdapter<String>
{
private final Context context;
private final ArrayList<String> values;
private final Map< String, SmbFile> obj;
static ArrayList<Boolean> checks=new ArrayList<Boolean>();
public imageCheckBoxAdapter(Context context,ArrayList<String> values,Map< String, SmbFile>obj) 
{
    super(context, R.layout.row_checkbox, values);
    this.context = context;
    this.values = values;
    this.obj=obj;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.row_checkbox, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.text1_check);
    textView.setText(values.get(position));
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon_image_check);
    try
    {
        if((obj.get(values.get(position)).isFile()))
        {
            imageView.setImageResource(R.drawable.view_file_icon);
        }
        else
        {
            imageView.setImageResource(R.drawable.view_folder_icon);
        }
    }
    catch (SmbException e) 
    {
        Toast.makeText(context,"Network error",Toast.LENGTH_SHORT).show();
        Log.d("id1", "error1");
        e.printStackTrace();
    }
    return rowView;
}
}

row_checkbox.xml

row_checkbox.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dp" >
    <CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="false" />
 <ImageView
    android:id="@+id/icon_image_check"
    android:layout_width="50px"
    android:layout_height="50px"
    android:layout_marginLeft="5px"
    android:layout_marginRight="20px"
    android:layout_marginTop="5px"
    android:src="@drawable/view_file_icon" >
    </ImageView>
 <TextView
    android:id="@+id/text1_check"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+id/label"
    android:textSize="30px" 
    android:typeface="sans">
    </TextView> 
</LinearLayout>

推荐答案

class imageCheckBoxAdapter extends ArrayAdapter<String> implements View.onClickListener
{
    private final Context context;
    private final ArrayList<String> values;
    private final Map< String, SmbFile> obj;
    private ArrayList<Boolean> checks=new ArrayList<Boolean>();

    public imageCheckBoxAdapter(Context context,ArrayList<String> values,Map< String, SmbFile>obj) 
    {
        super(context, R.layout.row_checkbox, values);
        this.context = context;
        this.values = values;
        this.obj=obj;

        for (int i = 0; i < values.size(); i++) {
            checks.add(i, false);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.row_checkbox, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.text1_check);
        CheckBox chk = (CheckBox) rowView.findViewById(R.id.checkBox1);
        textView.setText(values.get(position));
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon_image_check);
        try
        {
            if((obj.get(values.get(position)).isFile()))
            {
                imageView.setImageResource(R.drawable.view_file_icon);
            }
            else
            {
                imageView.setImageResource(R.drawable.view_folder_icon);
            }
        }
        catch (SmbException e) 
        {
            Toast.makeText(context,"Network error",Toast.LENGTH_SHORT).show();
            Log.d("id1", "error1");
            e.printStackTrace();
        }

        chk.setTag(Integer.valueOf(position));

        // Set a listener for the checkbox
        chk.setOnClickListener(this);

        //Sets the state of CB, since we have the list of checked CB
        chk.setChecked(checks.get(position));

        return rowView;
    }

    @Override
    public void onClick(View view) {
        Integer index = (Integer)view.getTag();
        boolean state = checks.get(index.intValue());

        checks.set(index.intValue(), !state);
    }
}

这篇关于android中的自定义复选框难度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆