Android:ListView子复制数据

时间:2015-01-22 10:59:29

标签: android listview baseadapter

我正在创建一个由三个孩子组成的ListView。 ListView高度和子高度设置相同,以便一次只能看到一个子项。我用一个只有一个LinearLayout的布局文件给孩子充气。在适配器类中,我将动态地向LinearLayout添加一些视图。现在我遇到的问题是当我滚动ListView时,第一个孩子的数据显示在另外两个孩子身上。这是我的适配器类:

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;
}

}

2 个答案:

答案 0 :(得分:3)

您没有正确使用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;
}

答案 1 :(得分:0)

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