我在项目中使用自定义列表视图,我遇到了一个问题。 我使用addview在listview中的行中添加了复选框,但在listview中滚动时取消选中复选框。 我按位置尝试了settag / gettag,但在我的情况下,一行有几个复选框,所以我不能使用位置。 我怎么解决这个问题?请帮帮我..
public View getView(final int position, View convertView, final ViewGroup parent) {
View itemView;
final ViewHolder viewHolder;
if (convertView == null) {
itemView = layoutInflater.inflate(R.layout.activity_delivering_partner_item, parent, false);
viewHolder = new ViewHolder();
final Deliveryltem deliveryltemPosition = epicerieDelivery_delivering_recipient.selectedDeliveryItem.get(position);
time = (TextView) itemView.findViewById(R.id. delivering_item_time);
name = (TextView) itemView.findViewById(R.id.delivering_item_name);
address = (TextView) itemView.findViewById(R.id.delivering_item_address);
goods = (TextView) itemView.findViewById(R.id.delivering_item_goods);
partner_linear = (LinearLayout) itemView.findViewById(R.id.delivering_partner_goods_linear);
LayoutInflater layoutInflater =
(LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
partner_linear.removeAllViews();
String purchase_name = "";
purchase_name = deliveringListActivity.purchase_name_arr.get(deliveringListActivity.purchase_num);
time.setText(deliveryltemPosition.shipping_time);
address.setText(deliveryltemPosition.recipient_address);
for(int k = 0; k< deliveringListActivity.partner_goods_arr.size(); k++){
final View addView = layoutInflater.inflate(R.layout.activity_delivering_partner_item_row, null);
TextView goods_name = (TextView) addView.findViewById(R.id.partner_goods_name_row);
TextView goods_ea = (TextView) addView.findViewById(R.id.partner_goods_ea_row);
viewHolder.checkbox = (CheckBox) addView.findViewById(R.id.partner_goods_chbox);
viewHolder.checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewHolder.checkbox.setId(position);
}
});
if(deliveryltemPosition.delivery_order_id.equals(deliveringListActivity.partner_goods_arr.get(k).goods_order_id)){
if(deliveringListActivity.partner_goods_arr.get(k).detail_purchase.equals(purchase_name)){
goods_name.setText(deliveringListActivity.partner_goods_arr.get(k).detail_product_name);
goods_ea.setText(deliveringListActivity.partner_goods_arr.get(k).detail_ea);
partner_linear.addView(addView);
}else{
}
}else{
}
}
return itemView;
}else{
itemView = convertView;
viewHolder = new ViewHolder();
if(epicerieDelivery_delivering_recipient.selectedDeliveryItem.size() != 0){
final Deliveryltem deliveryltemPosition = epicerieDelivery_delivering_recipient.selectedDeliveryItem.get(position);
time = (TextView) itemView.findViewById(R.id. delivering_item_time);
name = (TextView) itemView.findViewById(R.id.delivering_item_name);
address = (TextView) itemView.findViewById(R.id.delivering_item_address);
partner_linear = (LinearLayout) itemView.findViewById(R.id.delivering_partner_goods_linear);
LayoutInflater layoutInflater =
(LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
partner_linear.removeAllViews();
time.setText(deliveryltemPosition.shipping_time);
address.setText(deliveryltemPosition.recipient_address);
String purchase_name = "";
purchase_name = deliveringListActivity.purchase_name_arr.get(deliveringListActivity.purchase_num);
for(int k = 0; k< deliveringListActivity.partner_goods_arr.size(); k++){
final View addView = layoutInflater.inflate(R.layout.activity_delivering_partner_item_row, null);
TextView goods_name = (TextView) addView.findViewById(R.id.partner_goods_name_row);
TextView goods_ea = (TextView) addView.findViewById(R.id.partner_goods_ea_row);
viewHolder.checkbox = (CheckBox) addView.findViewById(R.id.partner_goods_chbox);
viewHolder.checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewHolder.checkbox.setId(position);
}
});
if(deliveryltemPosition.delivery_order_id.equals(deliveringListActivity.partner_goods_arr.get(k).goods_order_id)){
if(deliveringListActivity.partner_goods_arr.get(k).detail_purchase.equals(purchase_name)){
goods_name.setText(deliveringListActivity.partner_goods_arr.get(k).detail_product_name);
goods_ea.setText(deliveringListActivity.partner_goods_arr.get(k).detail_ea);
partner_linear.addView(addView);
}else{
}
}else{
}
}
}
return convertView;
}
}
答案 0 :(得分:1)
我认为您使用ViewHolder
的方法是错误的。在覆盖的getView
方法中,您需要更改如下例。
if (convertView == null) {
//Your stuffs
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
According to the documentation:
在滚动ListView期间,您的代码可能经常调用findViewById(),这会降低性能。即使适配器返回一个膨胀的视图以进行回收,您仍然需要查找元素并更新它们。围绕重复使用findViewById()的方法是使用&#34;视图持有者&#34;设计模式。
答案 1 :(得分:1)
这可以帮助您:
这是因为您没有维护复选框状态,这意味着当您单击复选框时需要将其存储在变量中时,此变量应该在模型类中,并且取决于您需要显示的变量状态复选框。
答案 2 :(得分:0)
删除你的if(convertView == null)else条件,或者记住你的pojo类检查更改并设置pojo类的复选框
答案 3 :(得分:0)
保存阵列中每个位置的复选框状态并使用此状态,将复选框设置为选中或不选中。请在此链接中查看此问题的解决方案:listview with checkbox status changed when scoll
答案 4 :(得分:0)
<强> 1。首先,您必须在适配器
中的全局中初始化布尔数组boolean[] checkedPositions = {false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false};
boolean size with false&#39; s是列表大小 如果你的列表有4行,那么像这样写 boolean [] checkedPositions = {false,false,false,false}
<强> 2。在复选框监听器上
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
checkedPositions[position] = true;
} else {
checkedPositions[position] = false;
}
}
});
第3。在此代码之后,您必须编写此代码
checkbox.setChecked(checkedPositions[position]);