Android的列表视图,视图单元格将只更新一次 [英] Android listview, view cell will only update once

查看:96
本文介绍了Android的列表视图,视图单元格将只更新一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的数组适配器列表视图,视图,用户可以用一些加号和减号按钮,我实现了

改变一个变量上下

现在的问题是,加/减键将再次改变看法,但该时间之后它不会更新,直到刷新视图时,以不同的方式。就像使用 listview.notifydatasetchanged()在外部操作。

当它更新,它表明了加/减键WERE的点击次数和更新变量,因为对数据的更新设置后立即将正确的变量视图

为什么不靠自己的个人看法更新?

 私有类MyListAdapter扩展ArrayAdapter<项目> {    私人的ArrayList<项目>项目;
    TextView的数量;
    INT I;    公共MyListAdapter(上下文的背景下,INT textViewResourceId,ArrayList的<项目>项目){
        超(背景下,textViewResourceId,项目);
        this.items =物品;
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        视图V = convertView;
        如果(V == NULL){
            LayoutInflater VI =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            V = vi.inflate(R.layout.list_scanneditems,NULL);
        }            最后一个项目allItems = items.get(位置);
        //声明细胞的各个部分
        TextView的产品=(TextView中)v.findViewById(R.id.productName);
        量=(TextView中)v.findViewById(R.id.listNum);
        TextView的类别=(TextView中)v.findViewById(R.id.categoryName);
        TextView的价格=(TextView中)v.findViewById(R.id.productCost);        product.setText(allItems.product);
        category.setText(allItems.category);
        price.setText($+ allItems.price);
        quantity.setText(将String.valueOf(allItems.quantity)); //可能想在这里做的天花板和地板的功能,如重量不得present        内就点击听众//设置属性
        //items.set(position,allItems.quantity);        ImageView的selectLeft =(ImageView的)v.findViewById(R.id.selectleft_list);
        ImageView的selectRight =(ImageView的)v.findViewById(R.id.selectright_list);        复选框itemCheckBox =(复选框)v.findViewById(R.id.itemCheckBox);        itemCheckBox.setChecked(selectallListener);        itemCheckBox.setOnCheckedChangeListener(新OnCheckedChangeListener(){            @覆盖
            公共无效onCheckedChanged(CompoundButton buttonView,
                    布尔器isChecked){
                // TODO自动生成方法存根
                如果(==器isChecked假放;&安培; selectallListener == true)而
                {
                    selectallListener = FALSE;                    复选框全选=(复选框)findViewById(R.id.selectAll);
                    selectAll.setChecked(假);
                }
            }        });
        selectLeft.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根                如果(allItems.quantity大于0)//我大于0,递减-1
                {
                    allItems.setQuantity(allItems.quantity-1);
                    quantity.setText(将String.valueOf(allItems.quantity));
                }
                别的//我小于或等于0
                {                    allItems.setQuantity(0);
                    quantity.setText(将String.valueOf(allItems.quantity));                    //把图标变成一个X的这里,如果为X删除的项                }            } //的onclick        }); // selectleft听者        selectRight.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根                allItems.setQuantity(allItems.quantity + 1);
                quantity.setText(将String.valueOf(allItems.quantity));            } //的onclick        }); // selectRight听者        //我需要在这里viewholder?        //设置监听器
        返回伏;
    }


解决方案

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

我觉得一定要像allItems变量。
在getView功能

最后TextView的数量= ...

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

解决方案

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 =…

这篇关于Android的列表视图,视图单元格将只更新一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆