另一个适配器内的Android List Adapter

时间:2012-11-10 18:38:37

标签: android list android-arrayadapter

所以我的想法是拥有一个列表而不是其中的另一个列表。 android分组不是我正在寻找的解决方案。我不希望他们分组。

我遇到内存泄漏和各种疯狂的问题。就像适配器多次遍历列表一样。下面是我的适配器。

public class LotteryAdapter extends ArrayAdapter<LotteryBall> {

Context context;

public LotteryAdapter(Context context, int resourceId,
        List<LotteryBall> items) {
    super(context, resourceId, items);
    this.context = context;
}

/*private view holder class*/
private class ViewHolder {
    LinearLayout ballpanel;
    LinearLayout jackpotpanel;
    TextView txtTitle;
    TextView txtBall;
    TextView txtJackpot;

}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    LotteryBall rowItem = getItem(position);
    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.mainrow, null);           
        convertView.setBackgroundResource(R.layout.greenbackground);    
        holder = new ViewHolder();     
        holder.txtTitle= (TextView) convertView.findViewById(R.id.LotteryTitle);           
        holder.ballpanel = (LinearLayout) convertView.findViewById(R.id.balllayout);
        holder.jackpotpanel = (LinearLayout) convertView.findViewById(R.id.jackpotlayout);
        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    holder.txtTitle.setText(rowItem.title);
    for(int i = 0;i < rowItem.balls.size();i++) 
      {
        Ball ball = rowItem.balls.get(i);
        if(ball != null){
            TextView tv = new TextView(context);  //<--problem with the context its causing the adapter to go crazy 
            tv.setText(ball.number);
            tv.setTextColor(Color.BLACK);
            tv.setBackgroundResource(R.drawable.yellowball);
            holder.ballpanel.addView(tv);

        }
      }
    //if jackpot
    TextView tvjackpot = new TextView(context);
    tvjackpot.setText("Current Jackpot: " + rowItem.amount);
    TextView tvnextdrawing = new TextView(context);
    tvjackpot.setText("Next Drawing: " + rowItem.nextdrawing);
    TextView tvpreviousjackpot = new TextView(context);
    tvjackpot.setText("Previous Jackpot: " + rowItem.lastamount);

    View ruler = new View(context); 
    ruler.setBackgroundColor(Color.WHITE);
    holder.jackpotpanel.addView(tvjackpot);
    holder.jackpotpanel.addView(ruler);
    holder.jackpotpanel.addView(tvnextdrawing);
    //holder.ballpanel.addView(ruler);
    holder.jackpotpanel.addView(tvpreviousjackpot);
    return convertView;
}

}

1 个答案:

答案 0 :(得分:3)

<强>重写

看起来您正在创建新的TextView球,无论现有球如何。您应该看到当前存在多少,然后添加或删除差异:

// Loop through the existing balls to update their text

if(holder.ballpanel.getChildCount() > rowItem.balls.size()) {
    // Remove extra balls
}
else if(holder.ballpanel.getChildCount() < rowItem.balls.size()) {
    // Add new balls
}