【问题标题】:Android List Adapter inside another adapter另一个适配器内的 Android 列表适配器
【发布时间】:2012-10-30 18:26:06
【问题描述】:

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

}

【问题讨论】:

    标签: android list android-arrayadapter


    【解决方案1】:

    重写

    看起来您正在创建新的 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
    }
    

    【讨论】:

    • 哈,我更改了答案以反映“真实”问题。 :)
    • 所以我需要检查它们是否存在?为什么它不断迭代列表?一遍又一遍
    • 我刚刚注意到您每次滚动时都会添加一个新的“累积奖金”布局...您需要在if(convertView == null) 中创建“累积奖金”布局并将其添加到您的 ViewHolder。但我不知道您所说的“为什么它不断地遍历列表?一遍又一遍”。
    • 我的意思是当我在手机上向下滚动列表时(在调试中)它会一遍又一遍地重建列表。我怎样才能让它只建立一次列表?
    • 哈哈,正如我之前写的那样。将所有代码放在 if(convertView == null) 中,它解决了我所有的问题。感谢您的帮助。
    猜你喜欢
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 2019-02-07
    • 1970-01-01
    相关资源
    最近更新 更多