当列表视图向下和向上滚动时,复选框改变了它的状态

时间:2014-01-28 13:09:17

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

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

我会发布我已经实现的代码,任何人都可以帮我解决这个问题吗?

 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);
      }
    }

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

执行以下操作时,不会更新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()调用),这将再次显示。

答案 2 :(得分:0)

你必须在getView中添加:

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

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