【问题标题】:Checkbox changed it's state when the list view is scrolled down and up当列表视图向下和向上滚动时,复选框更改了它的状态
【发布时间】:2014-01-28 13:09:17
【问题描述】:

我有三个复选框的三个侦听器,我应该检索所有三个复选框的值(真或假)并将它们存储在一个类中。稍后我将使用该类来检索值。但我的问题是当列表视图上下滚动时,动态创建的复选框值会发生变化。

我将发布我已经实现的代码,有人可以帮我摆脱这个吗?

 public class ListAdapter extends BaseAdapter {
    Context context;
    LayoutInflater layoutInflater;
    ArrayList<Product> objects;
    ImageHolder imageHolder;
    ImageView imageview;

    ListAdapter(Context context, ArrayList<Product> products) {
        this.context = context;
        objects = products;
        this.imageHolder = new ImageHolder(context);
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public Object getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = layoutInflater.inflate(R.layout.store_visit_list, parent, false);
        }

        Product p = getProduct(position);

        ((TextView) view.findViewById(R.id.product)).setText(p.name);
        imageview=(ImageView)view.findViewById(R.id.list_image);

        if(p.image.equals(null)||p.image.equals("null"))
            imageview.setImageResource(R.drawable.icon_default_sales_person);
        else
            imageHolder.DisplayImage(p.image, imageview);

        CheckBox cb1 = (CheckBox) view.findViewById(R.id.checkbox1);
        cb1.setTag(position);
        cb1.setChecked(p.is_order);

        CheckBox cb2 = (CheckBox) view.findViewById(R.id.checkbox2);
        cb2.setTag(position);
        cb2.setChecked(p.is_merchandising);

        CheckBox cb3 = (CheckBox) view.findViewById(R.id.checkbox3);
        cb3.setTag(position);
        cb3.setChecked(p.is_audit);
        return view;
    }

    Product getProduct(int position) {
        return ((Product) getItem(position));
    }

    public ArrayList<Product> getObjects(){
        return objects;
    }
    OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            getProduct((Integer) buttonView.getTag()).is_order=isChecked;
        }
    };
    OnCheckedChangeListener myCheckChangList1 = new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            getProduct((Integer) buttonView.getTag()).is_merchandising = isChecked;
        }
    };
    OnCheckedChangeListener myCheckChangList2 = new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            getProduct((Integer) buttonView.getTag()).is_audit = isChecked;

        }
    };
}
public class Product implements Serializable
{
      /**
     * 
     */
    private static final long serialVersionUID = 1L;
      String name;
      String position;
      int brandId;
      boolean is_order;
      boolean is_merchandising;
      boolean is_audit;
      String image;

      Product(String product_name,int brandid,boolean isorder,boolean ismerchandising, boolean isaudit,String photo) 
      {
        name = product_name;
        brandId=brandid;
        is_order = isorder;
        is_merchandising = ismerchandising;
        is_audit = isaudit;
        image=photo;
      }
      @Override
      public String toString() {
        return brandId+"#"+(is_audit ? 1 : 0 )+"#"+(is_merchandising ? 1 : 0)+"#"+(is_order ? 1 : 0);
      }
    }

【问题讨论】:

  • 为什么不接受我的回答?我先回答。

标签: android android-intent android-listview android-fragments


【解决方案1】:

您正在创建 OnCheckedChangeListener,但没有在复选框上设置它们,因此它们永远不会被调用。

【讨论】:

  • 一开始我没找到你。在 Kevinrob 回答之后,我尝试了我的代码并且它有效,因此我将他的答案标记为正确的答案。因此,我无法选择你们两个人的答案都是正确的!!!!
【解决方案2】:

你必须在getView中添加:

cb1.setOnCheckedChangeListener(myCheckChangList);
cb2.setOnCheckedChangeListener(myCheckChangList1);
cb3.setOnCheckedChangeListener(myCheckChangList2);

为简单起见,您可以直接用“产品”对象设置标签。

【讨论】:

  • 我应该这样声明吧? CheckBox cb2 = (CheckBox) view.findViewById(R.id.checkbox2); cb2.setTag(位置); cb2.setChecked(p.is_merchandising); cb2.setOnCheckedChangeListener(myCheckChangList1);
  • 将监听器放入getview方法后即可工作,感谢您的回复:)
  • 是的,其他复选框也是如此。如果不这样做,CheckBox 将永远不会触发任何侦听器。
【解决方案3】:

当你这样做时,你不会更新 arrayList 中的 Product 对象:

getProduct((Integer) buttonView.getTag()).is_order=isChecked;

在你的监听器中试试这个:

//Retrieve the product object from arrayList
Product p = getProduct((Integer) buttonView.getTag());
//Modify the object
p.is_order = isChecked;
//Update the ArrayList<Product> objects at desired position
objects.set((Integer) buttonView.getTag(), p);

注意,当listView中的元素不再显示时,它会被回收并重新创建(调用getView()),当它再次显示时。

【讨论】:

  • 所以不是 getProduct((Integer) buttonView.getTag()).is_order=isChecked;我应该设置以上三行以便 onclicklistener 将被更新吗?
猜你喜欢
  • 2017-11-17
  • 2017-02-16
  • 1970-01-01
  • 2016-06-03
  • 1970-01-01
  • 2012-10-16
  • 2023-03-21
  • 1970-01-01
  • 2020-12-21
相关资源
最近更新 更多