Listview重复项

时间:2017-10-13 08:07:39

标签: android android-layout listview android-activity

我尝试使用大量数据制作列表视图,但是当我构建时,listview上的项目数据会多次重复。

这是myJava 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;
}

任何人都可以解决?感谢

1 个答案:

答案 0 :(得分:2)

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

User