Android列表视图,查看单元格只会更新一次

时间:2011-11-13 02:49:41

标签: android listview view adapter

我有一个自定义数组适配器的列表视图,该视图允许用户使用我实现的一些加号和减号按钮来上下更改变量

现在的问题是加/减按钮会改变视图ONCE,但在此之后它将不会更新,直到视图以不同的方式刷新。比如在外部操作中使用listview.notifydatasetchanged()

当它更新时,它显示加/减按钮WERE注册点击并更新变量,因为在更新数据集时它立即将正确的变量放在视图中

为什么单个视图不能单独更新?

  private class MyListAdapter extends ArrayAdapter<Item> {

    private ArrayList<Item> items;
    TextView quantity;
    int i;

    public MyListAdapter(Context context, int textViewResourceId, ArrayList<Item> items) {
        super(context, textViewResourceId, items);
        this.items=items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_scanneditems, null);
        }

            final Item allItems = items.get(position);




        //declare all parts of the cell
        TextView product = (TextView)v.findViewById(R.id.productName);
        quantity = (TextView)v.findViewById(R.id.listNum);
        TextView category = (TextView)v.findViewById(R.id.categoryName);
        TextView price = (TextView)v.findViewById(R.id.productCost);

        product.setText(allItems.product);
        category.setText(allItems.category);
        price.setText("$" + allItems.price);
        quantity.setText(String.valueOf(allItems.quantity)); //might want to do ceiling and floor functions here, IF weight not present

        //set properties within on click listeners
        //items.set(position, allItems.quantity);

        ImageView selectLeft = (ImageView)v.findViewById(R.id.selectleft_list);
        ImageView selectRight = (ImageView)v.findViewById(R.id.selectright_list);

        CheckBox itemCheckBox = (CheckBox)v.findViewById(R.id.itemCheckBox);        

        itemCheckBox.setChecked(selectallListener);

        itemCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked==false && selectallListener==true)
                {
                    selectallListener=false;

                    CheckBox selectAll = (CheckBox)findViewById(R.id.selectAll);
                    selectAll.setChecked(false);
                }
            }

        });


        selectLeft.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                if(allItems.quantity > 0) //i greater than 0, decrement -1
                {
                    allItems.setQuantity(allItems.quantity-1);
                    quantity.setText(String.valueOf(allItems.quantity));
                }
                else //i less or equal to 0
                {

                    allItems.setQuantity(0);
                    quantity.setText(String.valueOf(allItems.quantity));

                    //turn icon into an X here an if it is X delete the item

                }

            }//onclick

        });//selectleft listener

        selectRight.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                allItems.setQuantity(allItems.quantity+1);
                quantity.setText(String.valueOf(allItems.quantity));

            }//onclick

        });//selectRight listener

        //do I need viewholder here?

        //set listeners


        return v;
    }

1 个答案:

答案 0 :(得分:1)

数量变量可能与发生点击的当前视图无关。现在它始终是在最后一次getView调用中创建/处理的TextView。

我认为必须像allItems变量一样。 在getView函数中

最终TextView数量= ...