使用listiview取消选中Onscroll选中的复选框
Onscroll selected checkbox getting unchecked using listiview
在我的应用程序中,我添加了 checkbox
和 ListView
,并且我还对 checkbox
进行了限制,用户不能 select 超过 5 checkbox
,但问题是滚动我的 selected checkbox
未选中以下是我的代码片段,任何人都可以帮助我解决这个问题
public class CustomAdapter extends BaseAdapter {
private LayoutInflater inflater = null;
Context context;
String rup = "\u20B9";
private ArrayList<ModelPooja> listData;
boolean checked[];
public CustomAdapterPooja(Context mainActivity, ArrayList<ModelPooja> listData) {
// TODO Auto-generated constructor stub
context = mainActivity;
this.listData = listData;
inflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
checked = new boolean[listData.size()];
for (int i = 0; i < checked.length; i++) {
checked[i] = listData.get(i).isselected;
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listData.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_item_poojaselection, null);
holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname);
holder.serviceprice = (TextView) convertView.findViewById(R.id.list_item_poojaprice);
holder.dayss = (TextView) convertView.findViewById(R.id.list_item_poojadays);
holder.txtseledates = (TextView) convertView.findViewById(R.id.selecteddatess);
holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.checks.setOnCheckedChangeListener(null);
holder.checks.setFocusable(false);
holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton cb, boolean b) {
if (checkMaxLimit()) {
if (listData.get(position).isselected && b) {
holder.checks.setChecked(false);
listData.get(position).isselected = false;
} else {
holder.checks.setChecked(false);
listData.get(position).isselected = false;
Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show();
}
} else {
if (b) {
listData.get(position).isselected = true;
} else {
listData.get(position).isselected = false;
}
}
}
});
if (listData.get(position).isselected()) {
holder.checks.setChecked(true);
} else {
holder.checks.setChecked(false);
}
holder.tv.setText(listData.get(position).getPOOJA_LISTING_NAME());
holder.dayss.setText(listData.get(position).getPOOJA_LISTING_DAYS());
holder.serviceprice.setText(rup + listData.get(position).getPOOJA_LISTING_AMOUNT());
return convertView;
}
public boolean checkMaxLimit() {
int countermax = 0;
for (int i = 0; i < checked.length; i++) {
checked[i] = false;
checked[i] = listData.get(i).isselected();
if (listData.get(i).isselected()) {
countermax++;
}
}
return countermax >= 5 ? true : false;
}
public class ViewHolder {
TextView tv;
TextView serviceprice;
public CheckBox checks;
public TextView dayss;
public TextView txtseledates;
}
}
在 adapter.If 中创建一个初始值为 false 的布尔数组,用户检查数组使数组的位置为真。取消选中使其为假。始终从该布尔数组设置复选框值。
这是因为当您从 phone 的上侧或下侧滚动您的列表视图时,列表视图的所有项目都将重新生成并设置为默认值。
为了避免这种情况,创建一个模型 class 来保存复选框的当前值(true 或 false),并通过从该模型实例调用 getter 方法来设置适配器中复选框的值。
例如:
model.java
public class AddressModel {
private boolean isChecked;
private String address;
private int alAddressDelete;
public AddressModel(){
}
public AddressModel(boolean isChecked, String address, int alAddressDelete) {
this.isChecked = isChecked;
this.address = address;
this.alAddressDelete = alAddressDelete;
}
public boolean getIsChecked() {
return isChecked;
}
public void setIsChecked(boolean isChecked) {
this.isChecked = isChecked;
}
}
在您的适配器 class 中,通过调用 setOnCheckedChangeListener()
中的 setIsChecked(boolean isChecked)
设置复选框的值。
在设置复选框值的位置调用 getIsChecked()
。
问题出在您的 getView() 方法中。您设置 OnCheckedChangeListener,然后将复选框设置为 true 或 false,这会在该侦听器中触发回调。您应该先设置复选框检查状态,然后再设置 OnCheckedChangeListener.
此外,那个 boolean checked[] 字段没有用,所以我稍微简化了你的代码:
public class CustomAdapter extends BaseAdapter {
private final LayoutInflater inflater;
private final Context context;
private List<ModelPooja> listData;
public CustomAdapter(Context mainActivity, List<ModelPooja> listData) {
context = mainActivity;
this.listData = listData;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_item_poojaselection, null);
holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname);
holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.checks.setOnCheckedChangeListener(null);
holder.checks.setFocusable(false);
if (listData.get(position).isselected) {
holder.checks.setChecked(true);
} else {
holder.checks.setChecked(false);
}
holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton cb, boolean b) {
if (checkMaxLimit()) {
if (listData.get(position).isselected && b) {
holder.checks.setChecked(false);
listData.get(position).isselected = false;
} else {
holder.checks.setChecked(false);
listData.get(position).isselected = false;
Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show();
}
} else {
if (b) {
listData.get(position).isselected = true;
} else {
listData.get(position).isselected = false;
}
}
}
});
holder.tv.setText(listData.get(position).getPOOJA_LISTING_NAME());
return convertView;
}
public boolean checkMaxLimit() {
int countermax = 0;
for(ModelPooja item : listData){
if(item.isselected){
countermax++;
}
}
return countermax >= 5;
}
public class ViewHolder {
TextView tv;
public CheckBox checks;
}
}
先调用setOnCheckedChangeListener()
,再调用setChecked()
,否则setChecked()
会触发前一个ViewHolder对象的setOnCheckedChangeListener()
.
在我的应用程序中,我添加了 checkbox
和 ListView
,并且我还对 checkbox
进行了限制,用户不能 select 超过 5 checkbox
,但问题是滚动我的 selected checkbox
未选中以下是我的代码片段,任何人都可以帮助我解决这个问题
public class CustomAdapter extends BaseAdapter {
private LayoutInflater inflater = null;
Context context;
String rup = "\u20B9";
private ArrayList<ModelPooja> listData;
boolean checked[];
public CustomAdapterPooja(Context mainActivity, ArrayList<ModelPooja> listData) {
// TODO Auto-generated constructor stub
context = mainActivity;
this.listData = listData;
inflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
checked = new boolean[listData.size()];
for (int i = 0; i < checked.length; i++) {
checked[i] = listData.get(i).isselected;
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listData.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_item_poojaselection, null);
holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname);
holder.serviceprice = (TextView) convertView.findViewById(R.id.list_item_poojaprice);
holder.dayss = (TextView) convertView.findViewById(R.id.list_item_poojadays);
holder.txtseledates = (TextView) convertView.findViewById(R.id.selecteddatess);
holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.checks.setOnCheckedChangeListener(null);
holder.checks.setFocusable(false);
holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton cb, boolean b) {
if (checkMaxLimit()) {
if (listData.get(position).isselected && b) {
holder.checks.setChecked(false);
listData.get(position).isselected = false;
} else {
holder.checks.setChecked(false);
listData.get(position).isselected = false;
Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show();
}
} else {
if (b) {
listData.get(position).isselected = true;
} else {
listData.get(position).isselected = false;
}
}
}
});
if (listData.get(position).isselected()) {
holder.checks.setChecked(true);
} else {
holder.checks.setChecked(false);
}
holder.tv.setText(listData.get(position).getPOOJA_LISTING_NAME());
holder.dayss.setText(listData.get(position).getPOOJA_LISTING_DAYS());
holder.serviceprice.setText(rup + listData.get(position).getPOOJA_LISTING_AMOUNT());
return convertView;
}
public boolean checkMaxLimit() {
int countermax = 0;
for (int i = 0; i < checked.length; i++) {
checked[i] = false;
checked[i] = listData.get(i).isselected();
if (listData.get(i).isselected()) {
countermax++;
}
}
return countermax >= 5 ? true : false;
}
public class ViewHolder {
TextView tv;
TextView serviceprice;
public CheckBox checks;
public TextView dayss;
public TextView txtseledates;
}
}
在 adapter.If 中创建一个初始值为 false 的布尔数组,用户检查数组使数组的位置为真。取消选中使其为假。始终从该布尔数组设置复选框值。
这是因为当您从 phone 的上侧或下侧滚动您的列表视图时,列表视图的所有项目都将重新生成并设置为默认值。 为了避免这种情况,创建一个模型 class 来保存复选框的当前值(true 或 false),并通过从该模型实例调用 getter 方法来设置适配器中复选框的值。 例如:
model.java
public class AddressModel {
private boolean isChecked;
private String address;
private int alAddressDelete;
public AddressModel(){
}
public AddressModel(boolean isChecked, String address, int alAddressDelete) {
this.isChecked = isChecked;
this.address = address;
this.alAddressDelete = alAddressDelete;
}
public boolean getIsChecked() {
return isChecked;
}
public void setIsChecked(boolean isChecked) {
this.isChecked = isChecked;
}
}
在您的适配器 class 中,通过调用 setOnCheckedChangeListener()
中的 setIsChecked(boolean isChecked)
设置复选框的值。
在设置复选框值的位置调用 getIsChecked()
。
问题出在您的 getView() 方法中。您设置 OnCheckedChangeListener,然后将复选框设置为 true 或 false,这会在该侦听器中触发回调。您应该先设置复选框检查状态,然后再设置 OnCheckedChangeListener.
此外,那个 boolean checked[] 字段没有用,所以我稍微简化了你的代码:
public class CustomAdapter extends BaseAdapter {
private final LayoutInflater inflater;
private final Context context;
private List<ModelPooja> listData;
public CustomAdapter(Context mainActivity, List<ModelPooja> listData) {
context = mainActivity;
this.listData = listData;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_item_poojaselection, null);
holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname);
holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.checks.setOnCheckedChangeListener(null);
holder.checks.setFocusable(false);
if (listData.get(position).isselected) {
holder.checks.setChecked(true);
} else {
holder.checks.setChecked(false);
}
holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton cb, boolean b) {
if (checkMaxLimit()) {
if (listData.get(position).isselected && b) {
holder.checks.setChecked(false);
listData.get(position).isselected = false;
} else {
holder.checks.setChecked(false);
listData.get(position).isselected = false;
Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show();
}
} else {
if (b) {
listData.get(position).isselected = true;
} else {
listData.get(position).isselected = false;
}
}
}
});
holder.tv.setText(listData.get(position).getPOOJA_LISTING_NAME());
return convertView;
}
public boolean checkMaxLimit() {
int countermax = 0;
for(ModelPooja item : listData){
if(item.isselected){
countermax++;
}
}
return countermax >= 5;
}
public class ViewHolder {
TextView tv;
public CheckBox checks;
}
}
先调用setOnCheckedChangeListener()
,再调用setChecked()
,否则setChecked()
会触发前一个ViewHolder对象的setOnCheckedChangeListener()
.