如何在适配器视图中添加了一个侦听复选框,机器人,ArrayAdapter,onCheckedChanged,OnCheckedChangeListener [英] how to add a listener for checkboxes in an adapter view, Android, ArrayAdapter, onCheckedChanged, OnCheckedChangeListener

查看:235
本文介绍了如何在适配器视图中添加了一个侦听复选框,机器人,ArrayAdapter,onCheckedChanged,OnCheckedChangeListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView,通过这样一个ArrayAdapter的填充小的XML子的看法。每个小视图只有两件事情里面,一个复选框和一个字符串标签旁边。

我想成立一​​个onCheckedChanged监听器来捕获用户的情况下,选中或取消选中复选框。

例如此处显示的监听器:

  listView.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){

 @覆盖
 公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){

 Toast.makeText(这一点,框已被选中,Toast.LENGTH_SHORT).show();

 }
 

}

我在哪里把听者code?和我怎么设置?

$ C $下一个ArrayAdapter:

 公共类MobileArrayAdapter扩展ArrayAdapter< CheckBoxInfo> {
    CheckBoxInfo []对象;
    上下文语境;
    INT textViewResourceId;

    公共MobileArrayAdapter(上下文的背景下,INT textViewResourceId,
        CheckBoxInfo []对象){
        超(背景下,textViewResourceId,对象);
        this.context =背景;
        this.textViewResourceId = textViewResourceId;
        this.objects =物体;

    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        查看row_layout_view = convertView;


            如果((row_layout_view == NULL)){


                LayoutInflater充气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row_layout_view = inflater.inflate(R.layout.row_layout,NULL);
            }

          // CheckBoxInfo项目= objects.get(位置); //为数组列表
            CheckBoxInfo项目=对象[位置]。

            如果(项目!= NULL){

            TextView中的TextView =(TextView中)row_layout_view.findViewById(R.id.textView1);
            复选框复选框=(复选框)row_layout_view.findViewById(R.id.checkBox1);

            如果(项目!= NULL){
            textView.setText(item.checkBoxName);
            checkBox.setChecked(item.checkBoxState);
               }
            }
            返回row_layout_view;
    }


}
 

解决方案

不要用你的例子 listView.setOnCheckedChangeListener onCheckedChanged code。

首先,对于复选框,你应该使用 setOnClickListener()而不是 setOnCheckedChangeListener()。你可以得到的onClick()函数内的选中状态。

二,把你的 setOnClickListener()里面的列表适配器的 getView()的功能。

示例

  checkBox.setOnClickListener(新OnClickListener(){
    @覆盖
    公共无效的onClick(查看为arg0){
        最终布尔=器isChecked checkBox.isChecked();
        //做的东西在这里。
    }
});
 

I have a listView that by way of an ArrayAdapter is populated by small xml sub views. each small view only has two things inside, a checkbox and a string label next to it.

i want to set an onCheckedChanged listener to capture the event of the user checking or unchecking the checkboxes.

for example the listener shown here:

 listView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

 @Override
 public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

 Toast.makeText(this, "box has been checked", Toast.LENGTH_SHORT).show();

 }

}

where do I put the listener code? and how do I set it up?

code for the ArrayAdapter:

  public class MobileArrayAdapter extends ArrayAdapter<CheckBoxInfo>{
    CheckBoxInfo[] objects;
    Context context;
    int textViewResourceId;

    public MobileArrayAdapter(Context context, int textViewResourceId,
        CheckBoxInfo[] objects) {
        super(context, textViewResourceId, objects);
        this.context = context;
        this.textViewResourceId = textViewResourceId;
        this.objects = objects;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row_layout_view = convertView;


            if ((row_layout_view == null)){


                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row_layout_view = inflater.inflate(R.layout.row_layout, null);
            }   

          //CheckBoxInfo item = objects.get(position);  // for arrayList
            CheckBoxInfo item = objects[position];

            if(item != null){

            TextView textView = (TextView) row_layout_view.findViewById(R.id.textView1);
            CheckBox checkBox = (CheckBox) row_layout_view.findViewById(R.id.checkBox1);

            if(item !=null){
            textView.setText(item.checkBoxName);
            checkBox.setChecked(item.checkBoxState);
               }
            }
            return row_layout_view;
    }


}

解决方案

Do not use your example listView.setOnCheckedChangeListener or onCheckedChanged code.

First of all, for CheckBox, you should use setOnClickListener() instead of setOnCheckedChangeListener(). You can get the checked state inside of the onClick() function.

Second, place your setOnClickListener() inside of the getView() function of the list adapter.

Example:

checkBox.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        final boolean isChecked = checkBox.isChecked();
        // Do something here.
    }
});

这篇关于如何在适配器视图中添加了一个侦听复选框,机器人,ArrayAdapter,onCheckedChanged,OnCheckedChangeListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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