我有一个食物表,每行我启动一个游标实例来获取所选测量单位的营养成分。我想知道如何关闭游标,因为它们需要不断打开,因为用户可以随时为每种食物选择不同的测量单位。
有什么想法吗?
修改
由于Quanturium要求启动游标实例的代码是以下方法:
protected void fillUnitsSpinner(String food) {
**final Cursor unitsCursor = mDataBaseHelper.getFoodMatches(food, null,
CURSOR_FOR_UNITS_SPINNER);**
final ArrayList<String> foodUnits = new ArrayList<String>();
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();
foodUnitsGlobal = foodUnits;
ArrayAdapter<CharSequence> unitsAdapter = new ArrayAdapter<CharSequence>(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<?> parent, View view,
int pos, long id) {
Log.i(TAG, "pos = " + pos);
TextView unitsText = (TextView) view.findViewById(R.id.f_a_units_item);
View row = (View) parent.getParent();
row.requestFocus();
defineViewsInFoodRow(row);
int pUnitsPos = -1;
String pShortcutUnit;
switch(pos) {
case MAIN_TABLE:
unitsCursor.moveToPosition(MAIN_TABLE);
break;
// other cases..
}
}
});