如何在适配器视图中为复选框添加侦听器,Android,ArrayAdapter,onCheckedChanged,OnCheckedChangeListener

时间:2013-04-11 05:56:04

标签: android listener adapter android-arrayadapter oncheckedchanged

enter image description here

我有一个listView,通过ArrayAdapter填充小xml子视图。每个小视图里面只有两个东西,一个复选框和旁边的字符串标签。

我想设置一个onCheckedChanged侦听器来捕获用户检查或取消选中复选框的事件。

例如此处显示的监听器:

 listView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

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

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

 }

}

我在哪里放置监听器代码?以及如何设置它?

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


}

4 个答案:

答案 0 :(得分:32)

请勿使用您的示例listView.setOnCheckedChangeListeneronCheckedChanged代码。

首先,对于CheckBox,您应该使用setOnClickListener()而不是setOnCheckedChangeListener()。您可以在onClick()函数内获取已检查状态。

其次,将setOnClickListener()放在列表适配器的getView()函数内。

示例

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

答案 1 :(得分:1)

谢谢大卫!关于HukeLau_DABA注释,我通过使用OnClickListener()内的CheckBox值更新列表视图对象来解决它。在我的情况下,我有一个地方列表,如下一个代码所示:

holder.placeCheckBox.setOnClickListener(new View.OnClickListener() {

       //please note that objPlace, position and holder must be declared 
       //as final inside the getView() function scope.   
          @Override
          public void onClick(View arg0) {
                final boolean isChecked = holder.placeCheckBox.isChecked();
                if (isChecked){
                   objPlace.setItemChecked("yes");
                }else{
                   objPlace.setItemChecked("no");
                }

                placesList.set(position,objPlace); //  updating the list with the updated object

            }
      });

答案 2 :(得分:1)

不要参考&#34; holder.placeCheckBox&#34;在你的onClickListener中,你应该引用onClick传递的视图并将其转换为CheckBox,所以代码应如下所示:

holder.placeCheckBox.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View arg0) {
            boolean isChecked = ((CheckBox)arg0).isChecked();
            if (isChecked){
               objPlace.setItemChecked("yes");
            }else{
               objPlace.setItemChecked("no");
            }

            placesList.set(position,objPlace); //  updating the list with the updated object

        }
  });

答案 3 :(得分:0)

你可以试试这个:

holder.placeCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        Log.d(TAG, "onCheckedChanged: " + holder.placeCheckBox.getText() + " " + (b ? "selected":"deselected"));
    }
});