【问题标题】:how to get the id of a checkbox inside an Adapter when it's checked?选中时如何获取适配器内复选框的ID?
【发布时间】:2023-04-08 09:31:01
【问题描述】:

我正在尝试在列表视图中实现一个复选框。 listView 是从 JSON 生成的。在我的适配器中,当我调用onCheckedChanged时,我想获取该复选框的 id,然后将其返回给服务器。如果我想用自己的构造函数为复选框设置 id 怎么办?

public class CheckListAdapter extends ArrayAdapter  implements CompoundButton.OnCheckedChangeListener{
List List =new ArrayList();
SparseBooleanArray mCheckStates;
boolean[] itemChecked;
    public CheckListAdapter(Context context, int resource) {
    super(context, resource);
        itemChecked = new boolean[10];
        mCheckStates = new SparseBooleanArray(10);
     }



public void add(@Nullable checkListItems object) {
    super.add(object);
    List.add(object);
}

@Override
public int getCount() {
    return List.size();
}

@Nullable
@Override
public Object getItem(int position) {
    return List.get(position);

}

public long getItemId(int position) {
    return 0;
}

@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
  View row;
  row = convertView;
 final checkListItemHolder h;
  if  ( row == null){
      LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      row = layoutInflater.inflate(R.layout.row_checklist,parent,false);
      h = new checkListItemHolder();
      h.tx_itemNo = (TextView) row.findViewById(R.id.checkListItemNO);
      h.tx_name= (TextView) row.findViewById(R.id.checkListItemName);
      h.checkBoxLab= (CheckBox) row.findViewById(R.id.checkBox);



      row.setTag(h);
  }
   else
   {
     h = (checkListItemHolder)row.getTag();
  }

     checkListItems L= (checkListItems)this.getItem(position);
     h.tx_name.setText(L.getItemName());
     Log.w("inja to adaptor","ta inja");
     h.tx_itemNo.setText(L.getItemNo());

 //    h.checkBoxLab.setChecked(false);
//     h.checkBoxLab.setChecked(checkedHolder[position]);
h.checkBoxLab.setOnCheckedChangeListener( new 
CompoundButton.OnCheckedChangeListener() {


        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
          //  checkedHolder[position] = isChecked;
           Log.w("yahoo","yahoo");

    }
});


     return row;

}

public boolean isChecked(int position) {
    return mCheckStates.get(position, false);
}

public void setChecked(int position, boolean isChecked) {
    mCheckStates.put(position, isChecked);

}

public void toggle(int position) {
    setChecked(position, !isChecked(position));

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

    mCheckStates.put((Integer) buttonView.getTag(), isChecked);

}

public boolean[] checkedHolder;

private void createCheckedHolder() {
    checkedHolder = new boolean[getCount()];
}


static class checkListItemHolder{

        TextView tx_name, tx_itemNo;
        CheckBox checkBoxLab;

}

【问题讨论】:

    标签: android listview android-adapter


    【解决方案1】:

    在这行h.checkBoxLab= (CheckBox) row.findViewById(R.id.checkBox);下添加这行h.checkBoxLab.setTag(position) 使用它从列表视图中删除该行:

    @Override
    public void onCheckedChanged(CompoundButton buttonView,
                                 boolean isChecked) {
        Integer index = (Integer) buttonView.getTag();
                        List.remove(index.intValue());  
                        notifyDataSetChanged();
    
    }
    

    请解释一下您到底想返回服务器什么?

    【讨论】:

    • 我不想删除该行。我需要获取复选框的 id
    • 如果您拥有的 List 中的对象包含您想要的 id,您可以使用 List.get(index.intValue()).getID() 访问它。如果您想将 id 设置为复选框,您可以使用 h.checkbox.setId(int); 然后在 onCheckChanged 中:buttonView.getId(); 。如果这些不是您要寻找的答案,请详细说明您的问题。
    • 我试过这个` if (isChecked) { Integer index = (Integer) buttonView.getId(); Log.w("雅虎",""+index); }' 但所有复选框的索引为零。
    • @HadiSamadbin 您在使用 getId() 之前设置了复选框 ID 吗?
    • 是这样的checkListItems L= (checkListItems)this.getItem(position);h.tx_name.setText(L.getItemName());h.tx_itemNo.setText(L.getItemNo());h.checkBoxLab.setId(L.getItemNoint());
    【解决方案2】:

    您可以简单地在 onCheckedChanged 方法中使用 buttonView.getId() 来获取复选框的 id。此视图始终引用检查状态已更改的复选框,如Doc 所述。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      • 1970-01-01
      • 2021-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多