Android: ListView child 重复数据

Android: ListView child duplicates data

我正在创建一个包含三个 child 的 ListView。 ListView 高度和 child 高度设置相同,因此一次只有一个 child 可见。我正在用一个只有一个 LinearLayout 的布局文件给 child 充气。在适配器 class 中,我正在向 LinearLayout 动态添加一些视图。现在我面临的问题是当我滚动 ListView 时,第一个 child 的数据显示在其他两个 child 中。这是我的适配器 class:

public class cListAdapter extends BaseAdapter {
private List<List<Date>> list;
private Context context;
public cListAdapter(Activity activity, List<List<Date>> list) {
    this.list = list;
    this.context = activity;
}
@Override
public int getCount() {
    return list.size();
}

@Override
public List<Date> getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

private static class ViewHolder{
    LinearLayout monthContainer;

}
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View view = convertView;
    ViewHolder holder;
    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view  = inflater.inflate(R.layout.list_item,parent,false);
        holder = new ViewHolder();

        view.setTag( holder );
    }else{
        holder = (ViewHolder)view.getTag();
    }
    holder.monthContainer = (LinearLayout)view.findViewById(R.id.month);
    List<Date> dates = getItem(position);
    Log.d("Position",""+position);
    Log.d("Dates",""+dates);
    Calendar c = new GregorianCalendar();
    c.setTime(dates.get(0));
    int day = c.get(Calendar.DAY_OF_WEEK);
    Log.d("Day",""+day);
    int i = 0;
    while( i < dates.size() ){
        LinearLayout row = new LinearLayout(context);
        row.setOrientation(LinearLayout.HORIZONTAL);

        for(int j = 0; j < 7; j++){
            Button b = new Button(context);
            b.setBackgroundResource(R.drawable.day_bg);
            b.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
            if(j<(day-1)) {
                b.setText("");
                row.addView(b);

            }else if(i < dates.size()) {
                b.setText(""+(i+1));
                row.addView(b);
                i++;
                day=0;
            }else{
                b.setText("");
                row.addView(b);
            }

        }
        holder.monthContainer.addView(row);
    }

    return view;
}

}

子视图在哪里?如果您需要子视图并将计数设置为 1 并加载数据,则应该使用可扩展适配器。将列表视图内的动态布局视图添加到持有者是很棘手的。您当前拥有的屏幕截图会有所帮助

您没有正确使用 ListView 中发生的视图回收。由于您将在每个项目上添加不同的数据,我建议您在添加新行之前从 monthContainer 中删除所有现有数据。我已经为您提供了以下代码。

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View view = convertView;
ViewHolder holder;
if(convertView == null){
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view  = inflater.inflate(R.layout.list_item,parent,false);
    holder = new ViewHolder();
    holder.monthContainer = (LinearLayout)view.findViewById(R.id.month);
    view.setTag( holder );
}else{
    holder = (ViewHolder)view.getTag();
}

holder.monthContainer.removeAllViews();
List<Date> dates = getItem(position);
Log.d("Position",""+position);
Log.d("Dates",""+dates);
Calendar c = new GregorianCalendar();
c.setTime(dates.get(0));
int day = c.get(Calendar.DAY_OF_WEEK);
Log.d("Day",""+day);
int i = 0;
while( i < dates.size() ){
    LinearLayout row = new LinearLayout(context);
    row.setOrientation(LinearLayout.HORIZONTAL);

    for(int j = 0; j < 7; j++){
        Button b = new Button(context);
        b.setBackgroundResource(R.drawable.day_bg);
        b.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
        if(j<(day-1)) {
            b.setText("");
            row.addView(b);

        }else if(i < dates.size()) {
            b.setText(""+(i+1));
            row.addView(b);
            i++;
            day=0;
        }else{
            b.setText("");
            row.addView(b);
        }

    }
    holder.monthContainer.addView(row);
}

return view;
}