【问题标题】:Android listview, view cell will only update onceAndroid listview,view cell只会更新一次
【发布时间】:2011-11-13 02:49:40
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: android listview view adapter


    【解决方案1】:

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

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

    最终的 TextView 数量 =…

    【讨论】:

    • 使用局部/函数变量代替类变量。就像 allItems (多么具有误导性的名称)变量一样。 ... //声明单元格的所有部分 TextView product = (TextView)v.findViewById(R.id.productName);最终 TextView 数量 = (TextView)v.findViewById(R.id.listNum); ...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多