buttonconvertview

How to increment and decrement textview value using two buttons in each row of custom listview in android


Here is my code, but when I run it, the output is same to all rows of listview that I don't want.

I want independent output for all rows.....

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

         final ViewHolder holder;


        Log.v("ConvertView", String.valueOf(position));

        if (convertView == null) {

            ViewHolder holder1 = new ViewHolder();
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = vi.inflate(R.layout.state_info, null);
            //  this.setPosition(position);

            holder = new ViewHolder();
            holder.position = getPosition();
            holder.state = stateList.get(position);
            holder.code = (TextView) convertView.findViewById(R.id.code);
            holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
            holder.num = (TextView) convertView.findViewById(R.id.up_down_button_value);
            holder.up = (ImageButton) convertView.findViewById(R.id.up_down_button_up);
            holder.down = (ImageButton) convertView.findViewById(R.id.up_down_button_down);

            holder.value = (TextView) convertView.findViewById(R.id.qnt1);

            convertView.setTag(holder);

            holder.name.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;
                    States state = (States) cb.getTag();
                    //Toast.makeText(getApplicationContext(),"Clicked on Checkbox: " + cb.getText() +" is " + cb.isChecked(),Toast.LENGTH_LONG).show();
                    state.setSelected(cb.isChecked());
                }
            });

          holder.up.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  _value++;
                  holder.num.setText(_value);
              }
          });

          holder.down.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  _value--;
                  holder.num.setText(_value);
              }
          });



        } else {
            holder = (ViewHolder) convertView.getTag();
        }



        States state = stateList.get(position);

        holder.code.setText(" (" + state.getCode() + ")");
        holder.name.setText(state.getName());
        holder.name.setChecked(state.isSelected());
        //holder.num.setText(state.getNum());

        holder.name.setTag(state);


        // checkIncButtonClick();
        // checkDecButtonClick();

        return convertView;

    }

Output :

       item     item quantity  : 2  b1 t1 b2

b1 and b2 are two buttons and t1 is textview of 1 row of listview and 2 is value of textview 1

Like that when I click on buttons value increase or decrease and finally that assign at place of 2


Solution

  • Just do few changes in code :

       public View getView(int position, View convertView, ViewGroup parent) {
    
            ViewHolder holder = null;
            //Log.v(“ConvertViewâ€, String.valueOf(position));
    
            if (convertView == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(
                        Context.LAYOUT_INFLATER_SERVICE);
                convertView = vi.inflate(R.layout.state_info, null);
    
                holder = new ViewHolder();
                holder.code = (TextView) convertView.findViewById(R.id.code);
                holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
                holder.num = (TextView) convertView.findViewById(R.id.up_down_button_value);
                holder.up = (ImageButton) convertView.findViewById(R.id.up_down_button_up);
                holder.down = (ImageButton) convertView.findViewById(R.id.up_down_button_down);
                convertView.setTag(holder);
    
                holder.name.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        CheckBox cb = (CheckBox) v;
                        States state = (States) cb.getTag();
    
                        state.setSelected(cb.isChecked());
                        if(!cb.isChecked())
                        {
                            RelativeLayout rl = (RelativeLayout) v.getParent();
                            TextView tv = (TextView) rl.findViewById(R.id.up_down_button_value);
                            tv.setText("0");
                            state.setNum(0);
                        }
    
                    }
                });
    
                holder.up.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v)
                    {
                        ImageButton ib = (ImageButton) v;
                        States state = (States) ib.getTag();
    
                        state.setUpSelected(ib.isClickable());
                        RelativeLayout rl = (RelativeLayout) v.getParent();
                        TextView tv = (TextView) rl.findViewById(R.id.up_down_button_value);
    
                        int cnt = Integer.valueOf(tv.getText().toString());
                        cnt += 1;
                        state.setNum(cnt);
    
    
                        tv.setText(""+(state.getNum()));
                    }
                });
    
                holder.down.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v)
                    {
                        ImageButton ib = (ImageButton) v;
                        States state = (States) ib.getTag();
    
                        state.setDownSelected(ib.isClickable());
                        RelativeLayout rl = (RelativeLayout) v.getParent();
                        TextView tv = (TextView) rl.findViewById(R.id.up_down_button_value);
                        int cnt = Integer.valueOf(tv.getText().toString());
                        cnt -=1;
                        if(cnt >= 0)
                        {
                            state.setNum(cnt);
                            tv.setText(""+(state.getNum()));
                        }
                        else
                        {
                            state.setNum(0);
                            tv.setText("0");
                        }
    
                    }
    
                });
            }
            else
            {
                holder = (ViewHolder) convertView.getTag();
            }
    
            States state = stateList.get(position);
            holder.code.setText("(" + state.getCode() + ")");
            holder.name.setText(state.getName());
            holder.name.setChecked(state.isSelected());
            holder.num.setText(""+(state.getNum()));
            holder.name.setTag(state);
            holder.up.setTag(state);
            holder.down.setTag(state);
            return convertView;
    
        }
    

    and add some method in Sates. java file :

    public void setNum(int num)
    {
        this.num = num;
    }
    
    public int getNum()
    {
        return num;
    }
    
    public void setUpSelected(boolean upSelected)
    {
        this.upSelected = upSelected;
    }
    
    public boolean isUpSelected()
    {
        return upSelected;
    }
    
    public void setDownSelected(boolean upSelected)
    {
        this.downSelected = downSelected;
    }
    
    public boolean isDownSelected()
    {
        return downSelected;
    }