title: Accessing cursor while iterating through another cursor fails to deliver data in time
body: <p>I have a database with foods that are displayed in a tablelayout with up to 20 lines of foods. The foods may have different measurement units that are retrieved through a union select query among three tables, and displayed in a spinner for each food in the table. When the user selects a unit of measurement in the spinner, the cursor is moved to the appropriate position so as to get the nutrients for the food's selected unit of measurement (through spinner's onItemSelected Listener). </p>
  
  <p>Well the problem is when I select a food record from a listview in order to update it in the food insertion display, when I try to recreate the food record iterating through the different foods in the record. When the food cursor is in a food row it calls the method that loads the different units of measurement for that particular food, and also selects the record's unit of measurement for that food, which I remind you, on selecting the unit the unitsCursor moves to the appropriate column to get the nutrients for that unit. </p>
  
  <p>Unfortunately the unitsCursor misses to deliver the data in time and the food row is partially filled when the foodCursor moves to the next lines. I cannot figure out why this is happening. Below is the method that loads the units spinner with the units of measurement and some code from the method that recreates the food record in the food insertion screen </p>
  
  <pre><code>protected void fillUnitsSpinner(String food) {
      unitsCursor = mDataBaseHelper.getFoodMatches(food, null, CURSOR_FOR_UNITS_SPINNER);
  
      foodUnits.clear();
      if(unitsCursor != null) {
          unitsCursor.moveToFirst();
          while(!unitsCursor.isAfterLast()) {
              // Get unit string
              String unit = unitsCursor.getString(unitsCursor.getColumnIndex(FoodDBHelper.UNITS));
              // Get practical units' extra columns if present
              String fingerTip = unitsCursor.getString(unitsCursor.getColumnIndex(
                      FoodDBHelper.FINGERTIP));
              // If practical units are present
              if(fingerTip != null) {
                  foodUnits.add(FINGERTIP.getpUnit());
                  foodUnits.add(FINGER.getpUnit());
                  foodUnits.add(HAND.getpUnit());
                  foodUnits.add(FIST.getpUnit());
                  foodUnits.add(CUP.getpUnit());
              }
              else foodUnits.add(unit);
              unitsCursor.moveToNext();
          }
          unitsCursor.moveToFirst();
  
          ArrayAdapter&lt;CharSequence&gt; unitsAdapter = new ArrayAdapter&lt;CharSequence&gt;(this,
                  R.layout.food_act_units_spinner_dropdown_item);
          unitsAdapter.addAll(foodUnits); // load adapter with food units
          unitsAdapter.setDropDownViewResource(R.layout.food_act_units_spinner_dropdown_item);
          mUnitsSpinner.setAdapter(unitsAdapter); // set adapter
          Log.i(TAG, "spinnerItem = " + unitsAdapter.getItem(foodUnits.indexOf(foodUnits.get(0))));
          // Select and set unit from the main table to the spinner
          if(!editRecord)
              mUnitsSpinner.setSelection(unitsAdapter.getPosition(foodUnits.get(0)));
  
          mUnitsSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
  
              @Override
              public void onItemSelected(AdapterView&lt;?&gt; parent, View view,
                      int pos, long id) {
                  Log.i(TAG, "pos = " + pos);
                  TextView unitsText = (TextView) view.findViewById(R.id.f_a_units_item);
                  int pUnitsPos = -1;
                  String pShortcutUnit;
  
                  switch(pos) {
                  case MAIN_TABLE:
                      unitsCursor.moveToPosition(MAIN_TABLE);
                      break;
                  // other cases
  
  
  
                  String unitsChoice = unitsText.getText().toString();
                  assigned = getValuesFromCursor(unitsCursor, unitsChoice);
              }
  
              @Override
              public void onNothingSelected(AdapterView&lt;?&gt; parent) {
                  // do nothing
              }
  
  
  
  
          });
      }
  }
  </code></pre>
  
  <p>and...</p>
  
  <pre><code>// Fill foodTable with records
      if(c != null &amp;&amp; c.moveToFirst()) {
          Log.i(TAG, "c.getCount = " + c.getCount());
          while(!c.isAfterLast() &amp;&amp; makeRow(c)) {
              c.moveToNext();
          }
  
      }       
      setListenersToFoodRows();
      foodTable.requestLayout();
  </code></pre>
  
  <p>In the makeRow(Cursor c) method there is a call to <code>fillUnitsSpinner(foodName);</code></p>