自定义适配器中重复的ListView项

时间:2013-07-03 04:42:25

标签: android listview custom-adapter

我正在尝试将动态textview添加到listview items.textviews可以是1-2或更多,具体取决于我成功添加textview的数据,但问题是在滚动时重复textviews。

我每次都在循环中创建textview的新对象。我知道android尝试重用现有视图的问题,但我每次都要添加新视图。

以下是自定义适配器中的代码:

public View getGroupView(int groupPosition, boolean isExpanded,
        View View, ViewGroup parent){
    final int pos=groupPosition;
    ManfVsCatRow row=new ManfVsCatRow();

    LayoutInflater inflater= context.getLayoutInflater();       
    groupView=(TableLayout)inflater.inflate(R.layout.salessummarycatvsmanfgroup, null);     
//  groupView.setBackgroundColor(Color.rgb(20,30,40));
    groupView.setBackgroundColor(R.drawable.bg);


        groupView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(context.getlist().isGroupExpanded(pos))
                context.getlist().collapseGroup(pos);
            else
                context.getlist().expandGroup(pos);             
        }
    });

    ClosingStock clstock=new ClosingStock();
    TableRow tr=new TableRow(context);
    TableRow.LayoutParams rowParams=new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
    tr.setLayoutParams(rowParams);
    tr.setWeightSum(1.0f);



    TextView space=new TextView(context);
    space.setLayoutParams(new TableRow.LayoutParams(0,TableRow.LayoutParams.WRAP_CONTENT,.1f));
    tr.addView(space);      

    TextView manufacturerTextView=new TextView(context);
    manufacturerTextView.setTypeface(Typeface.DEFAULT_BOLD);

    TableRow.LayoutParams categoryParams=new TableRow.LayoutParams(0,TableRow.LayoutParams.WRAP_CONTENT,.35f);
    categoryParams.setMargins(0, 5, 0, 5);  

    manufacturerTextView.setLayoutParams(categoryParams);        
    manufacturerTextView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);        
    try 
    {

    //  adapter.notifyDataSetChanged();



        manufacturerTextView.setText(groups[groupPosition]);    
        row.setItemsValue(manufacturerTextView.getText().toString());
        rowList.add(row);
        tr.addView(manufacturerTextView);

        Log.e("ManufaturerTextView", String.valueOf(groups[groupPosition]));

        int[] spGroupTotals=getGroupStockPointTotals(groupPosition);
        int spGroupTotalsTotal=0;
        for(int i=0;i<spArray.length();i++)
        {
            spGroupTotalsTotal+=spGroupTotals[i];
        }   

        TableRow.LayoutParams totalParams=new TableRow.LayoutParams(0,TableRow.LayoutParams.WRAP_CONTENT,.225f);
        totalParams.setMargins(0, 5, 0, 5);

        TextView tvTotal=new TextView(context);
        tvTotal.setLayoutParams(totalParams);
        tvTotal.setGravity(Gravity.CENTER);
        tvTotal.setText(String.valueOf(spGroupTotalsTotal));

        row=new ManfVsCatRow();
        row.setItemsValue(tvTotal.getText().toString());
        rowList.add(row);
        tr.addView(tvTotal);
        Log.e("SalesStockTextView", String.valueOf(spGroupTotalsTotal));


        TableRow.LayoutParams closingStockParams=new TableRow.LayoutParams(0,TableRow.LayoutParams.WRAP_CONTENT,.225f);
        closingStockParams.setMargins(0, 5, 0, 5);

        TextView closingStockTextView=new TextView(context);
        closingStockTextView.setLayoutParams(closingStockParams);
        closingStockTextView.setGravity(Gravity.CENTER);
        closingStockTextView.setText(getGroupClosingStock(groupPosition));


        row=new ManfVsCatRow();
        row.setItemsValue(closingStockTextView.getText().toString());
        rowList.add(row);
        tr.addView(closingStockTextView);
        Log.e("CLosing TextVIew",String.valueOf(getGroupClosingStock(groupPosition)));

    //  size++;

        groupView.addView(tr);


       for(int y=0;y<rowList.size();y++)
       {

           Log.e("Intern Here",String.valueOf(rowList.get(y)));

       }


        TextView spaceTextView=new TextView(context);
        spaceTextView.setLayoutParams(new TableRow.LayoutParams(0,TableRow.LayoutParams.WRAP_CONTENT,.1f));




    } catch (NumberFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    }

0 个答案:

没有答案