1

这是我的代码,但是当我运行它时,输出与我不想要的所有列表视图行相同。

我想要所有行的独立输出.....

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

    }

输出 :

       item     item quantity  : 2  b1 t1 b2

b1b2是两个按钮,是t11 行列表视图的文本视图,2 是文本视图 1 的值

就像我点击按钮时值增加或减少,最后分配到 2

4

1 回答 1

0

只需对代码进行一些更改:

   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;

    }

并在 Sates 中添加一些方法。java文件:

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;
}
于 2014-10-13T12:28:26.410 回答