title: Dynamicaly loading data to Gridview
body: <p>While i am working on gridview i faced following problems, any help will be appreciated, </p>
  
  <ol>
  <li><p>When I load data to my gridview it loads only first 3 items of the array but there are 18 items to be load. why it doesn't load other 15 items. (Log.i it shows all the 18 items in my LogCat).</p></li>
  <li><p>Since there are only 18 items belongs to the MainCategoryID=1, it should load only 18 grids, but this loads 28 grids(that is because there are 28 items inside responseJson). I want to limit it to 18 grids. (Inside the loaded 28 grids, there are 3 items loaded and there have repeated in couple of places and other grids are empty grids)</p></li>
  </ol>
  
  <p>PizzaFragment class</p>
  
  <pre><code>public class PizzaFragment extends ListFragment implements OnTaskCompleted {
  
      private TranslateAnimation anim;
      GridView grid;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.menu_grid_main, container, false);
  
          new PizzaMenuAsyncTask(getActivity(), this).execute();
  
  
          grid = (GridView) view.findViewById(R.id.grid);
          return view;
      }
  
      @Override
      public void onTaskCompleted(JSONArray responseJson) {
  
          try
          {
              String[] Description = new String[responseJson.length()];
              String[] ImageURL = new String[responseJson.length()];
              for (int i = 0; i &lt; responseJson.length(); i++)
              {
                  JSONObject object = responseJson.getJSONObject(i);
                  if ((object.getString("MainCategoryID")).equals("1"))
                  {
                      Log.i("MainCategoryID ", object.getString("ImageURL"));
                      ImageURL[i] = object.getString("ImageURL");
                      Log.i("MainCategoryID ", object.getString("Description"));
                      Description[i] = object.getString("Description");
  
                     CustomGrid adapter = new CustomGrid(getActivity(), Description, ImageURL);
                  grid.setAdapter(adapter);
                  }
  
              }
          }
              catch (JSONException e) {
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>CustomGrid</p>
  
  <pre><code>public class CustomGrid extends BaseAdapter {
  
      private Context mContext;
      private final String[] Description;
      private final String[] ImageURL;
  
      public CustomGrid(Context c, String[] Description, String[] ImageURL) {
          mContext = c;
          this.ImageURL = ImageURL;
          this.Description = Description;
      }
  
      @Override
      public int getCount() {
          return Description.length;
      }
  
      @Override
      public Object getItem(int position) {
          return null;
      }
  
      @Override
      public long getItemId(int position) {
          return 0;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          View grid;
  
          LayoutInflater inflater = (LayoutInflater) mContext
                  .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
          if (convertView == null) {
  
              grid = new View(mContext);
              grid = inflater.inflate(R.layout.fragment_pizza, null);
              TextView textView = (TextView) grid.findViewById(R.id.grid_text);
              ImageView imageView = (ImageView) grid
                      .findViewById(R.id.grid_image);
              textView.setText(Description[position]);
              Picasso.with(mContext).load(ImageURL[position]).into(imageView);
          } else {
              grid = (View) convertView;
          }
  
          return grid;
      }
  }
  </code></pre>
  
  <p>this is how my screen looks like (this is not the real image but this is how it looks)</p>
  
  <p><img src="https://i.stack.imgur.com/d6txx.png" alt="enter image description here"></p>