动态地将视图添加到listview行,在滚动时添加的视图会重复出现

时间:2012-07-18 05:17:07

标签: android listview

我有一个listview,我必须动态地将数据添加到linearLayout,因为它不是每行都有相同数据量的问题。

为此,我在LiveView Adapter中动态地将textView添加到Linear布局中。 现在滚动textview被添加!! 任何想法如何解决这个???

这就是我正在做的事。

static class ViewHolder
{       
    TextView itemName , itemQuantity , itemTotalPrice , attrKey , attrValue;
    CheckBox deleteCheckbox;
    ImageButton nextArrow;
    LinearLayout linearLayout , attrHolder;
}

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

    final ViewHolder holder;
    if(convertView == null)
    {
        convertView = mLayoutInflator.inflate(R.layout.basket_list,null);
        holder = new ViewHolder();
        holder.itemName = (TextView) convertView.findViewById(R.id.basket_list_item_name);
        holder.itemQuantity = (TextView) convertView.findViewById(R.id.basket_list_item_number);
        holder.itemTotalPrice = (TextView) convertView.findViewById(R.id.basket_list_total_amount);
        holder.nextArrow = (ImageButton) convertView.findViewById(R.id.basket_list_next_arraow);
        holder.deleteCheckbox = (CheckBox) convertView.findViewById(R.id.basket_list_checkbox);
        holder.linearLayout = (LinearLayout)convertView.findViewById(R.id.basket_list_atributes);
        convertView.setTag(holder);
    }else   
        holder = (ViewHolder) convertView.getTag();  

    attributes = items.get(position).getAttributeVOs();

    holder.itemName.setText(items.get(position).getItemName());
    holder.itemQuantity.setText("x"+items.get(position).getItemQuantity());
    holder.itemTotalPrice.setText("R  "+items.get(position).getItemTotalCost());

    if(CheckListActivity.flag)
        holder.deleteCheckbox.setVisibility(View.VISIBLE);
    else
        holder.deleteCheckbox.setVisibility(View.GONE);

    holder.deleteCheckbox.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            CheckBox cb = (CheckBox) v;
            System.out.println("CHECKED");
            if(cb.isChecked())
                holder.deleteCheckbox.setButtonDrawable(R.drawable.checkarrow);
            else
                holder.deleteCheckbox.setButtonDrawable(R.drawable.unchecked);

            checkedArray.put(position, cb.isChecked());             
        }
    });     

    holder.nextArrow.setOnClickListener(new OnClickListener() {         
        @Override
        public void onClick(View v) {
            //To next activity
        }
    });


    for(int i = 0 ; i < attributes.size() ; i ++)
    {
        LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        holder.attrHolder = new LinearLayout(context);
        holder.attrHolder.setOrientation(LinearLayout.HORIZONTAL);
        holder.attrHolder.setLayoutParams(lparams);         


        holder.attrKey = new TextView(context);
        holder.attrKey.setLayoutParams(lparams);
        holder.attrKey.setText(attributes.get(i).getKey());
        holder.attrValue = new TextView(context);
        holder.attrValue.setLayoutParams(lparams);
        holder.attrValue.setText(attributes.get(i).getValues().toString());

        holder.attrHolder.addView(holder.attrKey);
        holder.attrHolder.addView(holder.attrValue);                    
    }

    holder.linearLayout.addView(holder.attrHolder);
    return convertView;
}

2 个答案:

答案 0 :(得分:4)

已解决删除if(convertView == null){} else check。

答案 1 :(得分:0)

对于新添加行,不需要For Loop。您只需更改项目列表,Listview适配器具有通知方法以更改Listview中的更改。

您只需通过列表视图在Google中搜索通知更改。