【问题标题】:Listview Repeating ItemsListview 重复项
【发布时间】:2018-03-25 07:41:18
【问题描述】:

我尝试创建一个包含大量数据的列表视图,但是当我构建时,列表视图上的项目数据多次重复。

这是我的Java CustomAdapter

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();

        holder.Name = (TextView) convertView.findViewById(R.id.Recipe_Name);
        holder.Image_Block = (ImageView) convertView.findViewById(R.id.Recipe_Image);
        holder.Text_Recipe = (TextView) convertView.findViewById(R.id.Recipe_Text);
        holder.Text_Rarity = (TextView) convertView.findViewById(R.id.Recipe_Rarity);

        RowItem row_pos = rowItems.get(position);

        holder.Image_Block.setImageResource(row_pos.getImage_Block());
        holder.Name.setText(row_pos.getName());
        holder.Text_Recipe.setText(row_pos.getText_Recipe());
        holder.Text_Rarity.setText(row_pos.getText_Rarity());

        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }
    return convertView;
}

任何人都可以解决?谢谢

【问题讨论】:

  • 当然需要在创建或设置后将数据设置到holder。

标签: android android-layout listview android-activity


【解决方案1】:

您的数据不会重复——当您回收旧视图时,您不会将其初始化为新值。将设置值的代码(不是建立连接的代码)移到 if-else 语句之外。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();

        holder.Name = (TextView) convertView.findViewById(R.id.Recipe_Name);
        holder.Image_Block = (ImageView) convertView.findViewById(R.id.Recipe_Image);
        holder.Text_Recipe = (TextView) convertView.findViewById(R.id.Recipe_Text);
        holder.Text_Rarity = (TextView) convertView.findViewById(R.id.Recipe_Rarity);


        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }

    RowItem row_pos = rowItems.get(position);
    holder.Image_Block.setImageResource(row_pos.getImage_Block());
    holder.Name.setText(row_pos.getName());
    holder.Text_Recipe.setText(row_pos.getText_Recipe());
    holder.Text_Rarity.setText(row_pos.getText_Rarity());
    return convertView;
}

【讨论】:

    猜你喜欢
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    相关资源
    最近更新 更多