1

所以我的想法是有一个列表而不是其中的另一个列表。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;
}

}

4

1 回答 1

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
}
于 2012-11-10T18:43:55.177 回答