0

我有一个列表视图,我必须在其中动态地将数据添加到 linearLayout ,因为它不需要每一行都有相同数量的数据。

为此,我在 LiveView 适配器中将 textView 动态添加到线性布局。现在在滚动 textview 被 countinued 添加!任何想法如何解决这个问题???

这就是我正在做的事情。

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

2 回答 2

4

解决了删除 if(convertView == null) { }else 检查。

于 2012-07-18T06:36:02.503 回答
0

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

您只需通过 Listview 在 Google 中搜索通知更改。

于 2012-07-18T05:28:23.603 回答