GridView显示重复的项目

时间:2017-08-06 13:23:32

标签: android gridview android-adapter

我真的很惊讶,代码发生了什么,gridView项目每次都在第11个位置显示重复,有时也显示第12个位置。

enter image description here

你可以看到"桌面"网格已经填充了两次。

网格图标&来自常量文件的名称

public static final String[] values = {
            DESKTOP,
            LAPTOP,
            SERVER,
            PRINTER,
            NETWORK_SUPPORT,
            AIR_CONTITION,
            CCTV_CAMERA,
            ALL_IN_ONE_PC,
            HOME_CLEANING,
            APPLICANCE_REPAIR,
            ELECTRICITY,
            PLUMBING
    };

    public static final int[] images = {
            R.drawable.desktop_pc_icon,
            R.drawable.laptop_pc_icon,
            R.drawable.server_icon,
            R.drawable.printer_icon,
            R.drawable.network_support_icon,
            R.drawable.aircondition_icon,
            R.drawable.cctv_camera_icon,
            R.drawable.all_in_one_pc_icon,
            R.drawable.home_cleaning_icon,
            R.drawable.appliance_repair_icon,
            R.drawable.electrical_icon,
            R.drawable.plumbing_icon
    };

网格采用者设置(在onCreate方法中)

 final GridView mainGrid = (GridView) findViewById(R.id.main_grid);
        mainGrid.setAdapter(new GridAdapter(MainActivity.this, AppConstants.productsId, AppConstants.values, AppConstants.images));

网格适配器

public class GridAdapter extends BaseAdapter {

    private final String[] productsId;
    private final String[] values;
    private final int[] images;
    private Context context;
    private LayoutInflater layoutInflater;

    public GridAdapter(final Context context, final String [] productsId, final String[] values, final int[] images) {
        this.context = context;
        this.productsId = productsId;
        this.values = values;
        this.images = images;
    }

    @Override
    public int getCount() {
        return productsId.length;
    }

    @Override
    public Object getItem(final int i) {
        return values[i];
    }

    @Override
    public long getItemId(final int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, final ViewGroup viewGroup) {
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (view == null) {
            view = layoutInflater.inflate(R.layout.single_grid_item, null);

            //Grid Text
            final TextView gridText = view.findViewById(R.id.grid_text);
            gridText.setText(values[i]);

            //Grid Image
            final ImageView gridImage = view.findViewById(R.id.grid_image);
            gridImage.setImageResource(images[i]);
        }
        return view;
    }
}

1 个答案:

答案 0 :(得分:1)

如下所示更改getView,每次调用getView时都应更新该项,但在您的情况下,仅在视图为空时才更新。所以这个问题是因为重用视图而发生的。

@Override
public View getView(final int i, View view, final ViewGroup viewGroup) {
    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (view == null) {
        view = layoutInflater.inflate(R.layout.single_grid_item, null);


    }

    //Grid Text
    final TextView gridText = view.findViewById(R.id.grid_text);
    gridText.setText(values[i]);

    //Grid Image
    final ImageView gridImage = view.findViewById(R.id.grid_image);
    gridImage.setImageResource(images[i]);
    return view;
}