在Android的定制复选框困难复选框、困难、Android

2023-09-13 00:57:09 作者:给爷跪下

如何滚动时修改此imageCheckBoxAdapter code以保持复选框的状态(即所有被检查应选中即使滚动后,又在检查变量需要存放在数组中的复选框)?

 类imageCheckBoxAdapter扩展ArrayAdapter<字符串>
{
私人最终上下文的背景下;
私人最终的ArrayList<字符串>值;
私人最终地图<字符串,SmbFile> OBJ;
静态的ArrayList<布尔>检查=新的ArrayList<布尔>();
公共imageCheckBoxAdapter(上下文的背景下,ArrayList的<字符串>的价值观,地图<字符串,SmbFile> OBJ)
{
    超(背景下,R.layout.row_checkbox,价值观);
    this.context =背景;
    this.values​​ =值;
    this.obj = OBJ;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
    LayoutInflater充气=(LayoutInflater)上下文
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    查看rowView = inflater.inflate(R.layout.row_checkbox,父母,假);
    TextView中的TextView =(TextView中)rowView.findViewById(R.id.text1_check);
    textView.setText(values​​.get(位置));
    ImageView的ImageView的=(ImageView的)rowView.findViewById(R.id.icon_image_check);
    尝试
    {
        如果((obj.get(values​​.get(位置))。ISFILE()))
        {
            imageView.setImageResource(R.drawable.view_file_icon);
        }
        其他
        {
            imageView.setImageResource(R.drawable.view_folder_icon);
        }
    }
    赶上(SmbException E)
    {
        Toast.makeText(背景下,网络错误,Toast.LENGTH_SHORT).show();
        Log.d(ID1,ERROR1);
        e.printStackTrace();
    }
    返回rowView;
}
}
 

row_checkbox.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:填充=5DP>
    <复选框
    机器人:ID =@ + ID / checkBox1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:检查=FALSE/>
 < ImageView的
    机器人:ID =@ + ID / icon_image_check
    机器人:layout_width =50像素
    机器人:layout_height =50像素
    机器人:layout_marginLeft =5像素
    机器人:layout_marginRight =20像素
    机器人:layout_marginTop =5像素
    机器人:SRC =@可绘制/ view_file_icon>
    < / ImageView的>
 <的TextView
    机器人:ID =@ + ID / text1_check
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文本=@ + /标记
    机器人:TEXTSIZE =30像素
    机器人:字体=SANS>
    < / TextView的>
< / LinearLayout中>
 

解决方案

 类imageCheckBoxAdapter扩展ArrayAdapter<字符串>实现View.onClickListener
{
私人最终上下文的背景下;
私人最终的ArrayList<字符串>值;
私人最终地图<字符串,SmbFile> OBJ;
私人的ArrayList<布尔>检查=新的ArrayList<布尔>();
公共imageCheckBoxAdapter(上下文的背景下,ArrayList的<字符串>的价值观,地图<字符串,SmbFile> OBJ)
{
    超(背景下,R.layout.row_checkbox,价值观);
    this.context =背景;
    this.values​​ =值;
    this.obj = OBJ;

    的for(int i = 0; I< values​​.size();我++){
        checks.add(我,FALSE);
    }
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
    LayoutInflater充气=(LayoutInflater)上下文
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    查看rowView = inflater.inflate(R.layout.row_checkbox,父母,假);
    TextView中的TextView =(TextView中)rowView.findViewById(R.id.text1_check);
    复选框CHK =(复选框)rowView.findViewById(R.id.checkBox1);
    textView.setText(values​​.get(位置));
    ImageView的ImageView的=(ImageView的)rowView.findViewById(R.id.icon_image_check);
    尝试
    {
        如果((obj.get(values​​.get(位置))。ISFILE()))
        {
            imageView.setImageResource(R.drawable.view_file_icon);
        }
        其他
        {
            imageView.setImageResource(R.drawable.view_folder_icon);
        }
    }
    赶上(SmbException E)
    {
        Toast.makeText(背景下,网络错误,Toast.LENGTH_SHORT).show();
        Log.d(ID1,ERROR1);
        e.printStackTrace();
    }

    chk.setTag(Integer.valueOf(位置));

    //为复选框,设置监听器
    chk.setOnClickListener(本);

    //设置CB的状态,因为我们已经检查CB名单
    chk.setChecked(checks.get(位置));

    返回rowView;
}
   @覆盖
   公共无效的onClick(视图查看){
     整数指数=(整数)view.getTag();
     布尔州= checks.get(index.intValue());

     checks.set(index.intValue(),状态!);
   }
}
 
android studio勾选框,android studio创建android项目 5 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

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

If this helps you, please mark this post as an answer.

Thanks..

 
精彩推荐
图片推荐