2017-08-06 119 views
1

我真的很驚訝,代碼發生了什麼,gridView項目在第11個位置每次都顯示重複,有時候還有第12個位置。GridView顯示重複的項目

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

更改您的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; 
}