在Android列表视图中限制复选框选择使用Preseed 3位置列表位置= 1复选框自动删除
Limit checkbox selection in Android listivew user Preseed 3 poition list position = 1 checkbox is automatic remove
我想将 checkbox
selection in android listivew
限制为例如当用户 [=22] 时只有 2 个复选框应该被 selected =]ed 3 position chackbox position one checkboc was unchecked.
用户可以select列表中的任意两个复选框,如何实现?这是我的要点文件:
public class ListAdapter extends ArrayAdapter<ListModel> {
private ListModel listModel;
private int selectedItemCounter = 0;
public ListAdapter(Context context, List<ListModel> listModels) {
super(context, 0, listModels);
}
@NonNull
@Override
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
listModel = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_view_item_row, parent, false);
}
TextView productTitle = (TextView) convertView.findViewById(R.id.product_title);
TextView productDescription = (TextView) convertView.findViewById(R.id.product_description);
final CheckBox icon_right = (CheckBox) convertView.findViewById(R.id.icon_right);
LinearLayout Listener = (LinearLayout) convertView.findViewById(R.id.list_view_listener);
Listener.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
icon_right.performClick();
}
});
icon_right.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
selectedItemCounter++;
} else {
selectedItemCounter--;
}
if (selectedItemCounter >= 3) {
buttonView.setChecked(false);
selectedItemCounter--;
notifyDataSetChanged();
}
}
});
icon_right.setChecked(false);
productTitle.setText(listModel.product);
productDescription.setText(listModel.productDetails);
// icon_right.setImageResource(R.drawable.icon_save);
return convertView;
}
}
试试这个:
public class ListAdapter extends ArrayAdapter<ListModel>{
private ListModel listModel;
private ArrayList<Integer> selectedPos = new ArrayList<Integer>();
public ListAdapter(Context context, List<ListModel> listModels) {
super(context, 0, listModels);
}
@NonNull
@Override
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
listModel = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_view_item_row, parent, false);
}
TextView productTitle = (TextView) convertView.findViewById(R.id.product_title);
TextView productDescription = (TextView) convertView.findViewById(R.id.product_description);
final CheckBox icon_right = (CheckBox) convertView.findViewById(R.id.icon_right);
LinearLayout Listener = (LinearLayout) convertView.findViewById(R.id.list_view_listener);
Listener.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
icon_right.performClick();
}
});
icon_right.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
if (cb.isChecked()) {
selectedPos.add((Integer)v.getTag());
} else {
selectedPos.remove((Integer)v.getTag());
}
if (selectedPos.size() >= 3) {
selectedPos.remove(0);
notifyDataSetChanged();
}
}
});
productTitle.setText(listModel.product);
productDescription.setText(listModel.productDetails);
if(selectedPos.contains(position)){
icon_right.setChecked(true);
}else{
icon_right.setChecked(false);
}
icon_right.setTag(position);
return convertView;
}}
希望有所帮助!
我想将 checkbox
selection in android listivew
限制为例如当用户 [=22] 时只有 2 个复选框应该被 selected =]ed 3 position chackbox position one checkboc was unchecked.
用户可以select列表中的任意两个复选框,如何实现?这是我的要点文件:
public class ListAdapter extends ArrayAdapter<ListModel> {
private ListModel listModel;
private int selectedItemCounter = 0;
public ListAdapter(Context context, List<ListModel> listModels) {
super(context, 0, listModels);
}
@NonNull
@Override
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
listModel = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_view_item_row, parent, false);
}
TextView productTitle = (TextView) convertView.findViewById(R.id.product_title);
TextView productDescription = (TextView) convertView.findViewById(R.id.product_description);
final CheckBox icon_right = (CheckBox) convertView.findViewById(R.id.icon_right);
LinearLayout Listener = (LinearLayout) convertView.findViewById(R.id.list_view_listener);
Listener.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
icon_right.performClick();
}
});
icon_right.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
selectedItemCounter++;
} else {
selectedItemCounter--;
}
if (selectedItemCounter >= 3) {
buttonView.setChecked(false);
selectedItemCounter--;
notifyDataSetChanged();
}
}
});
icon_right.setChecked(false);
productTitle.setText(listModel.product);
productDescription.setText(listModel.productDetails);
// icon_right.setImageResource(R.drawable.icon_save);
return convertView;
}
}
试试这个:
public class ListAdapter extends ArrayAdapter<ListModel>{
private ListModel listModel;
private ArrayList<Integer> selectedPos = new ArrayList<Integer>();
public ListAdapter(Context context, List<ListModel> listModels) {
super(context, 0, listModels);
}
@NonNull
@Override
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
listModel = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_view_item_row, parent, false);
}
TextView productTitle = (TextView) convertView.findViewById(R.id.product_title);
TextView productDescription = (TextView) convertView.findViewById(R.id.product_description);
final CheckBox icon_right = (CheckBox) convertView.findViewById(R.id.icon_right);
LinearLayout Listener = (LinearLayout) convertView.findViewById(R.id.list_view_listener);
Listener.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
icon_right.performClick();
}
});
icon_right.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
if (cb.isChecked()) {
selectedPos.add((Integer)v.getTag());
} else {
selectedPos.remove((Integer)v.getTag());
}
if (selectedPos.size() >= 3) {
selectedPos.remove(0);
notifyDataSetChanged();
}
}
});
productTitle.setText(listModel.product);
productDescription.setText(listModel.productDetails);
if(selectedPos.contains(position)){
icon_right.setChecked(true);
}else{
icon_right.setChecked(false);
}
icon_right.setTag(position);
return convertView;
}}
希望有所帮助!