【问题标题】:Android - Change text of button in a selected rowAndroid - 更改所选行中按钮的文本
【发布时间】:2013-05-13 02:29:30
【问题描述】:

我已经尝试了所有方法,但没有任何效果。 我有一个这样的列表视图:

   [ checkbox ] | [textview] | [button]

我要做的是仅在选中 [ checkbox ] 的行中更改 [button] 的文本。 这是有问题的代码块:

public View getView(int position, View convertView, ViewGroup parent){
        final ViewHolder vh;
        final View conv;

        if(convertView == null){
            LayoutInflater vi=(LayoutInflater)pContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=vi.inflate(R.layout.rest_dishes_item, null);
            conv=convertView;
            final Button b[];
            b=new Button[itens.size()];
            vh=new ViewHolder();
            vh.txt=(TextView)convertView.findViewById(R.id.dish_name);
            vh.checkBox=(CheckBox) convertView.findViewById(R.id.dish_item);
            vh.qnt=(Button) convertView.findViewById(R.id.qnt);

            vh.quantidade=new Quantidade[itens.size()];
            for(int i=0;i<itens.size();i++){
                b[i]=(Button)conv.findViewById(R.id.qnt);
                vh.quantidade[i].quantidade=1;
                vh.quantidade[i].order=i;
            }
            vh.qnt.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    for(int i=0;i<itens.size();i++){
                        Titem item=itens.get(i);
                        if(item.isCheck()){
                            vh.quantidade[i].quantidade++;
                            //Log.d("teste",i+""+vh.quantidade[i].quantidade);
                            b[i].setText(String.valueOf(vh.quantidade[i].quantidade));
                        }
                        else if(!item.isCheck()){
                            Log.d("teste",i+"1");
                            b[i].setText(String.valueOf(1));
                        }
                    }

                }

            });
                    //(Button) convertView.findViewById(R.id.qnt);



            convertView.setTag(vh);


            vh.checkBox.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    CheckBox box=(CheckBox) arg0;
                    Titem item=(Titem)box.getTag();

                    item.setCheck(box.isChecked());
                }
            });
        }else{
            vh=(ViewHolder)convertView.getTag();
        }

        Titem item=itemList.get(position);



        vh.txt.setText(item.getName());
        vh.checkBox.setText(item.getName());
        vh.checkBox.setChecked(item.isCheck());
        vh.checkBox.setTag(item);

        //Log.d("teste","chegou aqui");

        return convertView;
    }

使用此代码,按钮的文本根本不会改变,无论我单击哪个按钮,他的文本仍然是“1”。

不带线:

b[i].setText(String.valueOf(1));

选中行的按钮已按我的意愿更改了他的文本。但是当我单击未选中行的按钮时,单击的按钮的文本已更改。

也许我正在做一些愚蠢的事情,或者只是在算法中遗漏了一些东西,此时我被卡住了。

请帮忙。 谢谢。

【问题讨论】:

  • 为什么要使用按钮数组??

标签: java android algorithm android-listview android-button


【解决方案1】:

听起来你很困惑,getView() 每个 可见 项都会执行一次。这是你的代码:

        vh.qnt=(Button) convertView.findViewById(R.id.qnt);

        vh.quantidade=new Quantidade[itens.size()];
        for(int i=0;i<itens.size();i++){
            b[i]=(Button)conv.findViewById(R.id.qnt);
            vh.quantidade[i].quantidade=1;
            vh.quantidade[i].order=i;
        }

vh.qntb[i] 的所有实例引用完全相同的按钮 (R.id.qnt)在正在呈现的行中

在更新vh 视图的其余部分时,您还需要在 if/else 之后更新按钮的文本。

【讨论】:

  • 所以我需要在xml文件中创建多个Button?
【解决方案2】:

我做到了!! 我使用了 setTag() 和 getTag(),它们允许我对不同的按钮使用相同的 id (R.id.qnt)。代码如下:

int aux=0; //before onCreate()

。 . .

vh.qnt.setTag(aux);//set a tag for each button in row
aux++;             //this will be the number of rows
vh.qnt.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View arg0) {

                    int index=(Integer)vh.qnt.getTag();
                    Titem item=itens.get(index);
                    if(item.isCheck()){
                        vh.qnt.setText(String.valueOf(vh.quantidade[index]++));

                    }
                }

            });

感谢@dmon 的提示,你让我看到了“相同 id”的事实。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 2021-10-19
    • 2017-10-03
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多