Android 如何获取适配器中复选框的id,当它';检查过了吗?
我试图在列表视图中实现一个复选框。listView是从JSON生成的。在我的适配器中,当我调用onCheckedChanged时,我想获取该复选框的id,然后将其返回给服务器。如果我想用自己的构造函数为复选框设置一个id怎么办Android 如何获取适配器中复选框的id,当它';检查过了吗?,android,listview,android-adapter,Android,Listview,Android Adapter,我试图在列表视图中实现一个复选框。listView是从JSON生成的。在我的适配器中,当我调用onCheckedChanged时,我想获取该复选框的id,然后将其返回给服务器。如果我想用自己的构造函数为复选框设置一个id怎么办 public class CheckListAdapter extends ArrayAdapter implements CompoundButton.OnCheckedChangeListener{ List List =new ArrayList(); Spars
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;
}
将此行
h.checkBoxLab.setTag(位置)
添加到此行h.checkBoxLab=(复选框)行.findviewbyd(R.id.CheckBox)下代码>
使用此选项从列表视图中删除行:
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Integer index = (Integer) buttonView.getTag();
List.remove(index.intValue());
notifyDataSetChanged();
}
请解释您希望返回服务器的具体内容?您只需在onCheckedChanged方法中使用按钮view.getId()
即可获取复选框的id。此视图始终引用其检查状态已更改的复选框。我不想删除该行。我需要获取选中框的id如果列表中的对象包含所需的id,则可以使用List.get(index.intValue()).getID()
访问该对象。如果要将id设置为复选框,可以使用h.checkbox.setId(int)然后在onCheckChanged:buttonView.getId()代码>。如果这些不是您想要的答案,请详细解释您的问题。我尝试了以下方法`If(isChecked){Integer index=(Integer)buttonView.getId();Log.w(“yahoo”,“”+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())代码>