当前位置: 首页 > 面试题库 >

android中的自定义复选框难度

明星剑
2023-03-14
问题内容

如何修改此imageCheckBoxAdapter代码以在滚动时保持checkBox的状态(也就是说,即使滚动后所有选中的复选框也应保持选中状态。选中的变量也需要存储在数组中)?

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


 类似资料:
  • 我有一个带有的自定义对话框。在我的上,我有一个按钮,当我按下它时,对话框就会显示出来,我可以从中选择日期。我希望选定的日期显示在我的中的上。我的代码如下: 这是我的主上包含的对话框的代码: 我从中获取日、月和年,并使用 在我的片段中,我使用下面的代码调用我的对话框: 当我运行我的应用程序时,我在以下行中得到一个nullPointerException: 我做错了什么?提前致谢

  • 我有一个小问题与复选框集成在谷歌地图控件。 在谷歌地图API v3文档中,它说你可以使用“自定义控件”(https://developers.google.com/maps/documentation/javascript/examples/control-custom),但没有关于下拉框的内容。我在网上冲浪,发现了布里亚纳·沙利文的这个例子: http://vislab-ccom.unh.edu

  • 在自定义arrayAdapter中实现自定义getFilter时遇到问题。实际上,我不知道如何实现它。尝试了各种代码,但仍然没有成功。这是我的自定义阵列适配器。 这是ListTO课程。 这是布局图。 这里的搜索关键字来自“inputSearch”编辑文本。 这是文本更改的侦听器。 谢谢

  • 本文向大家介绍Android 自定义对话框 showSetPwdDialog,包括了Android 自定义对话框 showSetPwdDialog的使用技巧和注意事项,需要的朋友参考一下 样式如下所示: 布局: layout   dialog_set_pwd.xml 状态选择器: drawable   btn_blue_selector.xml   btn_white_selector.xml 引

  • 我可以更改复选框的颜色,但似乎无法更改文本标签的颜色。我想用CSS做这个。这是我的密码。 我尝试添加带有复选框的标签选择器,如许多其他网站上提到的,但它不起作用。。 我非常感谢你的帮助,我在这方面花了很多时间,如果能找到解决办法,我会感到非常欣慰。提前谢谢!!

  • 我有一个RecyclerView,它有一个复选框和文本视图。数字10,20,30,40。。。文本视图中应显示至500。选中的复选框应在文本视图中添加与复选框对应的数字。例如,如果用户仅检查值10,textView将显示10。如果用户同时选中20,则TextView将显示30(20 10)。如果用户再次取消选中10,TextView将显示20,依此类推。当我点击复选框时,一些随机复选框也被选中。我在