Android listview、view cell只会更新一次

发布于 2024-12-15 11:32:02 字数 3759 浏览 2 评论 0原文

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

问题是加号/减号按钮将更改视图一次,但在那之后它在以不同方式刷新视图之前不会更新。就像在外部操作中使用 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;
    }

I have a listview with custom array adapter, the view allows the user to change a variable up and down with some plus and minus buttons I implemented

now the problem is that the plus/minus buttons will change the view ONCE, but after that time it won't update until the view is refreshed a different way. like using an listview.notifydatasetchanged() in an external operation.

when it updates, it shows that the plus/minus buttons WERE registering clicks and updating the variable, because on update of the data set it immediately puts the correct variable in the view

why isn't the individual view updating on its own?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人爱我别走 2024-12-22 11:32:02

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

我认为一定像 allItems 变量。
在 getView 函数中

最终 TextView 数量 =...

quantity variable could be unrelated to the current view in which clicks happens. Now it always is the TextView created/processed in last getView call.

I think must be like allItems variable.
in getView function

final TextView quantity =…

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文