我有两个旋转器,第二个旋转器上的信息基于第一个旋转器选择示例旋转器 1 蛋白质奶制品如果旋转器 1 设置有蛋白质然后旋转器 2 鸡蛋鸡如果旋转器 1 设置有水果然后旋转器 2 苹果橙色
选择的最后一个值 SPINNER 2 ORANGE
现在我想保留 SPINNER1 和 SPINNER2 的最后选择的值,我正在使用 SharedPreferences 来完成此操作,它按预期对 SPINNER 1 FRUITS Position 2 工作,但是对于 SPINNER2,即使位置正确,它也不会设置最后选择的值位置 1 当它被调用时,onStart();
它选择数组 APPLE 上的第一个值
任何帮助,将不胜感激
谢谢
public class TEST extends AppCompatActivity implements View.OnTouchListener {
SharedPreferences sharedPrefStoreInfo, sharedPrefGetInfo;
public String keyRow1, getBackInformationRow1;
String concatenateInfoRow1, currentDateString, concatenateInfoTotals, getBackInformationTotals,
getBackInformationParseTotals[],
getBackInformationParseRow1[]
;
// declare adaptors to bind with spinners
ArrayAdapter<String> spinnerFoodCategoryRow1Adapter;
// declare the references for the UI elements
public static Spinner spinnerFoodCategoryRow1,
spinnerFoodNameRow1,;
private String[] oilsFoodInformation, fruitsFoodInformation, foodCategoryInformation;
TextView day, date, textViewProteinRow1, textViewCarbsRow1, textViewFatRow1, textViewCaloriesRow1,
textViewDailyTotalProtein, textViewDailyTotalCarbs, textViewDailyTotalFat, textViewDailyTotalCalories
;
EditText editRow1;
String textInputRow1, textInputRow1ResultProtein, textInputRow1ResultCarbs, textInputRow1ResultFat,
textInputRow1ResultCalories,
dailyTotalProtein, dailyTotalCarbs, dailyTotalFat, dailyTotalCalories
;
int gramsEditTextInputParseRow1;
double resultIntProteinRow1, resultIntCaloriesRow1, resultIntCarbsRow1, resultIntFatRow1,;
double proteinInfoRule3Row1, carbsInfoRule3Row1, fatInfoRule3Row1, caloriesInfoRule3Row1,;
public String foodNameTempRow1,;
double canolaProtein = 0;
double canolaCarbs = 0;
double canolaFat = 1;
double canolaCalories = 8.84;
double coconutOilProtein = 0;
double coconutOilCarbs = 0;
double coconutOilFat = 1;
double coconutOilCalories = 8.62;
double cornProtein = 0;
double cornCarbs = 0;
double cornFat = 1;
double cornCalories = 9;
double applesProtein = 0;
double applesCarbs = 0;
double applesFat = 0;
double applesCalories = 0;
double apricotsProtein = 0;
double apricotsCarbs = 0;
double apricotsFat = 0;
double apricotsCalories = 0;
double bananasProtein = 0;
double bananasCarbs = 0;
double bananasFat = 0;
double bananasCalories = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.food_tracker_layout); -
editRow1 = (EditText) findViewById(R.id.editRow1);
textViewProteinRow1 = (TextView) findViewById(R.id.textViewProteinR1);
textViewCarbsRow1 = (TextView) findViewById(R.id.textViewCarbsR1);
textViewFatRow1 = (TextView) findViewById(R.id.textViewFatR1);
textViewCaloriesRow1 = (TextView) findViewById(R.id.textViewCaloriesR1);
spinnerFoodCategoryRow1 = (Spinner) findViewById(R.id.spinner1R1);
spinnerFoodNameRow1 = (Spinner) findViewById(R.id.spinner2R1);
initializeSpinnerFoodCategoryAdapters();
// CHANGE THE FONT SIZE OF SPINNER FOOD CATEGORY
spinnerFoodCategoryRow1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Change the selected item's text color
((TextView) view).setTextColor(Color.BLACK);
((TextView) view).setTextSize(12);
getSelectedFoodCategoryRow1(); // **
// THIS IS TO CHANGE THE FONT SIZE OF SPINNER FOOD NAME
spinnerFoodNameRow1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Change the selected item's text color
((TextView) view).setTextColor(Color.BLACK);
((TextView) view).setTextSize(10);
getSelectedFoodNameRow1();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});// end
} // end
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}// End Main
public void initializeSpinnerFoodCategoryAdapters() {
Resources res = getResources();
spinnerFoodCategoryRow1Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
res.getStringArray(R.array.SpinnerArrayInfoFoodCategory));
spinnerFoodCategoryRow1.setAdapter(spinnerFoodCategoryRow1Adapter);
spinnerFoodCategoryRow2Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
res.getStringArray(R.array.SpinnerArrayInfoFoodCategory));
spinnerFoodCategoryRow2.setAdapter(spinnerFoodCategoryRow2Adapter);
}
public String getSelectedFoodCategoryRow1() {
Resources res = getResources();
String selectedspinnerFoodCategoryRow1 = (String) spinnerFoodCategoryRow1.getSelectedItem();
sharedPrefStoreInfo = PreferenceManager.getDefaultSharedPreferences(this);
// FOOD NAME OILS ROW1
if (spinnerFoodCategoryRow1.getSelectedItemPosition() == 0) {
spinnerFoodNameRow1Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
res.getStringArray(R.array.SpinnerArrayInfoOils));
spinnerFoodNameRow1.setAdapter(spinnerFoodNameRow1Adapter);
int selectedPosition = spinnerFoodCategoryRow1.getSelectedItemPosition();
SharedPreferences.Editor editor = sharedPrefStoreInfo.edit();
editor.putInt("ROW1", selectedPosition);
editor.commit();
return selectedspinnerFoodCategoryRow1;
}
if (spinnerFoodCategoryRow1.getSelectedItemPosition() == 2) {
spinnerFoodNameRow1Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
res.getStringArray(R.array.SpinnerArrayInfoFruits));
spinnerFoodNameRow1.setAdapter(spinnerFoodNameRow1Adapter);
int selectedPosition = spinnerFoodCategoryRow1.getSelectedItemPosition();
SharedPreferences.Editor editor = sharedPrefStoreInfo.edit();
editor.putInt("ROW1", selectedPosition);
editor.commit();
return selectedspinnerFoodCategoryRow1;
}
return selectedspinnerFoodCategoryRow1;
}
// RETRIEVE INFO FOODNAME FROM THE SPINNER ROW1
public String getSelectedFoodNameRow1() {
Resources res = getResources();
String selectedspinnerFoodCategoryRow1 = (String) spinnerFoodCategoryRow1.getSelectedItem();
String selectedspinnerFoodNameRow1 = (String) spinnerFoodNameRow1.getSelectedItem();
foodCategoryInformation = res.getStringArray(R.array.SpinnerArrayInfoFoodCategory);
oilsFoodInformation = res.getStringArray(R.array.SpinnerArrayInfoOils);
fruitsFoodInformation = res.getStringArray(R.array.SpinnerArrayInfoFruits);
for (int n = 0; n < foodCategoryInformation.length; n++) {
// SELECTS INFORMATION ON THE FOOD CATEGORY SPINNER
if (spinnerFoodCategoryRow1.getSelectedItemPosition() == n) {
// SELECT TYPE OF FOOD CATEGORY
switch (selectedspinnerFoodCategoryRow1) {
case "OILS":
foodCategoryTempRow1 = selectedspinnerFoodCategoryRow1;
// ITERATE THE ARRAY TO SELECT THE FOOD NAME ITEM IN OILS
for (int i = 0; i < oilsFoodInformation.length; i++) {
// SELECT FOOD NAME BASED ON POSITION IN DAIRY
if (spinnerFoodNameRow1.getSelectedItemPosition() == i) {
SharedPreferences.Editor editor = sharedPrefStoreInfo.edit();
editor.putInt("ROW1-OILS", i);
editor.commit();
if (selectedspinnerFoodNameRow1.equals("Canola")) {
// ASSIGN THE FOOD NAME SELECTED TO A
// foodNameTempRow1 VARIABLE
foodNameTempRow1 = selectedspinnerFoodNameRow1;
// ASSIGN INFO TO VARIABLES THAT WILL BE USED ON
// THE CALCULATION CLICK BUTTON
proteinInfoRule3Row1 = canolaProtein;
carbsInfoRule3Row1 = canolaCarbs;
fatInfoRule3Row1 = canolaFat;
caloriesInfoRule3Row1 = canolaCalories;
} // end IF CANOLA
if (selectedspinnerFoodNameRow1.equals("Coconut Oil")) {
// ASSIGN THE FOOD NAME SELECTED TO A
// foodNameTempRow1 VARIABLE
foodNameTempRow1 = selectedspinnerFoodNameRow1;
proteinInfoRule3Row1 = coconutOilProtein;
carbsInfoRule3Row1 = coconutOilCarbs;
fatInfoRule3Row1 = coconutOilFat;
caloriesInfoRule3Row1 = coconutOilCalories;
} // end IF Coconut Oil
if (selectedspinnerFoodNameRow1.equals("Corn")) {
// ASSIGN THE FOOD NAME SELECTED TO A
// foodNameTempRow1 VARIABLE
foodNameTempRow1 = selectedspinnerFoodNameRow1;
proteinInfoRule3Row1 = cornProtein;
carbsInfoRule3Row1 = cornCarbs;
fatInfoRule3Row1 = cornFat;
caloriesInfoRule3Row1 = cornCalories;
} // end IF Corn
} // end if OILS POSITION
} // end for OILS
break;
case "FRUITS":
// ASSIGN THE FOOD CATEGORY SELECTED TO A
// foodCategoryTempRow1 VARIABLE
foodCategoryTempRow1 = selectedspinnerFoodCategoryRow1;
// ITERATE THE ARRAY TO SELECT THE FOOD NAME ITEM IN DAIRY
for (int i = 0; i < fruitsFoodInformation.length; i++) {
// SELECT FOOD NAME BASED ON POSITION IN DAIRY
if (spinnerFoodNameRow1.getSelectedItemPosition() == i) {
if (selectedspinnerFoodNameRow1.equals("Apples")) {
// ASSIGN THE FOOD SELECTED TO A
// foodNameTempRow1Dairy VARIABLE
foodNameTempRow1 = selectedspinnerFoodNameRow1;
// ASSIGN INFO TO VARIABLES THAT WILL BE USED ON
// THE CALCULATION CLICK BUTTON
proteinInfoRule3Row1 = applesProtein;
carbsInfoRule3Row1 = applesCarbs;
fatInfoRule3Row1 = applesFat;
caloriesInfoRule3Row1 = applesCalories;
} // end if Apples
if (selectedspinnerFoodNameRow1.equals("Apricots")) {
foodNameTempRow1 = selectedspinnerFoodNameRow1;
proteinInfoRule3Row1 = apricotsProtein;
carbsInfoRule3Row1 = apricotsCarbs;
fatInfoRule3Row1 = apricotsFat;
caloriesInfoRule3Row1 = apricotsCalories;
} // end if Apricots
if (selectedspinnerFoodNameRow1.equals("Bananas")) {
foodNameTempRow1 = selectedspinnerFoodNameRow1;
proteinInfoRule3Row1 = bananasProtein;
carbsInfoRule3Row1 = bananasCarbs;
fatInfoRule3Row1 = bananasFat;
caloriesInfoRule3Row1 = bananasCalories;
} // end if Bananas
} // end if FRUITS POSITION
} // end for FRUITS
break;
}// END SWITCH
} // IF OUTER
} // OUTER FOR LOOP
return selectedspinnerFoodNameRow1;
}// end METHOD GET FOODNAME ROW1
public boolean onTouch(View v, MotionEvent ev) {
boolean handledHere = false;
final int action = ev.getAction();
final int evX = (int) ev.getX();
final int evY = (int) ev.getY();
int nextImage = -1;
ImageView imageView = (ImageView) v.findViewById(R.id.image);
if (imageView == null)
return false;
Integer tagNum = (Integer) imageView.getTag();
int currentResource = (tagNum == null) ? R.drawable.background_food_tracker : tagNum.intValue();
switch (action) {
case MotionEvent.ACTION_DOWN:
if (currentResource == R.drawable.background_food_tracker) {
handledHere = true;
} else
handledHere = true;
break;
case MotionEvent.ACTION_UP:
int touchColor = getHotspotColor(R.id.image_areas, evX, evY);
ColorTouchAreas ct = new ColorTouchAreas();
int tolerance = 25;
nextImage = R.drawable.background_food_tracker;
if (ct.closeMatch(Color.RED, touchColor, tolerance))
{
// GET INPUT EDIT INFORMATION ENTERED IN GRAMS
textInputRow1 = editRow1.getText().toString();
// VALIDATION NOT EMPTY FIELD EDITTEXT
if (textInputRow1.equals(""))
{
MainActivity.mpButtonSumit.start(); // sound
tToast("Please Enter Weight Of Grams For " + foodNameTempRow1);
}
else {
// SELECTS THE TYPE OF FOOD CATEGORY
switch (foodCategoryTempRow1) {
case "OILS":
// tToast("CLICK OILS ROW1"); // TESTING
for (int i = 0; i < oilsFoodInformation.length; i++) {
if (foodNameTempRow1.equals(oilsFoodInformation[i])) {
MainActivity.mpButtonSumit.start(); // sound
// tToast("STRING VALUE= " + textInputRow1);
if (!"".equals(textInputRow1)) {
gramsEditTextInputParseRow1 = Integer.parseInt(textInputRow1);
// tToast("INT VALUE= " +
// String.valueOf(gramsEditTextInputParseRow1));
// //TESTING
resultIntProteinRow1 = (double) (gramsEditTextInputParseRow1
* proteinInfoRule3Row1);
resultIntProteinRow1 = (int) Math.round(resultIntProteinRow1);
// MAKE CALCULATION CARBS
resultIntCarbsRow1 = (double) (gramsEditTextInputParseRow1 * carbsInfoRule3Row1);
resultIntCarbsRow1 = (int) Math.round(resultIntCarbsRow1);
// MAKE CALCULATION FAT
resultIntFatRow1 = (double) (gramsEditTextInputParseRow1 * fatInfoRule3Row1);
resultIntFatRow1 = (int) Math.round(resultIntFatRow1);
// MAKE CALCULATION CALORIES
resultIntCaloriesRow1 = (double) (gramsEditTextInputParseRow1
* caloriesInfoRule3Row1);
resultIntCaloriesRow1 = (int) Math.round(resultIntCaloriesRow1);
// PARSE BACK FROM INTEGER TO STRING TO SET
// THE TEXT WHEN CLICK BUTTON IS CLICED
textInputRow1ResultProtein = Integer.toString((int) resultIntProteinRow1);
textInputRow1ResultCarbs = Integer.toString((int) resultIntCarbsRow1);
textInputRow1ResultFat = Integer.toString((int) resultIntFatRow1);
textInputRow1ResultCalories = Integer.toString((int) resultIntCaloriesRow1);
// CONCATENATE INFORMATION IN ROW 1
concatenateInfoRow1 = textInputRow1ResultProtein + " - " + textInputRow1ResultCarbs
+ " - " + textInputRow1ResultFat + " - " + textInputRow1ResultCalories
;
// CREATE KEY ROW1
keyRow1 = currentDateString + "-" + "keyRow1"; /
sharedPrefStoreInfo = PreferenceManager.getDefaultSharedPreferences(this);
// now get Editor
SharedPreferences.Editor editor = sharedPrefStoreInfo.edit();
// put your value the key would be current
// date + keyRow1
editor.putString(keyRow1, concatenateInfoRow1);
// commits your edits
editor.commit();
} // end if initial edittext is empty
textViewProteinRow1.setText(textInputRow1ResultProtein);
// CARBS GRAMS
textViewCarbsRow1.setText(textInputRow1ResultCarbs);
// FAT GRAMS
textViewFatRow1.setText(textInputRow1ResultFat);
// CALORIES GRAMS
textViewCaloriesRow1.setText(textInputRow1ResultCalories);
} // end if Oils
} // end for Oils
break;
case "FRUITS":
for (int i = 0; i < fruitsFoodInformation.length; i++) {
if (foodNameTempRow1.equals(fruitsFoodInformation[i])) {
MainActivity.mpButtonSumit.start(); // sound
// tToast("STRING VALUE= " + textInputRow1);
if (!"".equals(textInputRow1)) {
gramsEditTextInputParseRow1 = Integer.parseInt(textInputRow1);
resultIntProteinRow1 = (double) (gramsEditTextInputParseRow1
* proteinInfoRule3Row1);
resultIntProteinRow1 = (int) Math.round(resultIntProteinRow1); // ROUND
// UP
// VALUE
// MAKE CALCULATION CARBS
resultIntCarbsRow1 = (double) (gramsEditTextInputParseRow1 * carbsInfoRule3Row1);
resultIntCarbsRow1 = (int) Math.round(resultIntCarbsRow1); // ROUND
resultIntFatRow1 = (double) (gramsEditTextInputParseRow1 * fatInfoRule3Row1);
resultIntFatRow1 = (int) Math.round(resultIntFatRow1); // ROUND
resultIntCaloriesRow1 = (double) (gramsEditTextInputParseRow1
* caloriesInfoRule3Row1);
resultIntCaloriesRow1 = (int) Math.round(resultIntCaloriesRow1); //
textInputRow1ResultProtein = Integer.toString((int) resultIntProteinRow1);
textInputRow1ResultCarbs = Integer.toString((int) resultIntCarbsRow1);
textInputRow1ResultFat = Integer.toString((int) resultIntFatRow1);
textInputRow1ResultCalories = Integer.toString((int) resultIntCaloriesRow1);
// CONCATENATE INFORMATION IN ROW 1
concatenateInfoRow1 = textInputRow1ResultProtein + " - " + textInputRow1ResultCarbs
+ " - " + textInputRow1ResultFat + " - " + textInputRow1ResultCalories
;
// CREATE KEY ROW1
keyRow1 = currentDateString + "-" + "keyRow1";
sharedPrefStoreInfo = PreferenceManager.getDefaultSharedPreferences(this);
// now get Editor
SharedPreferences.Editor editor = sharedPrefStoreInfo.edit();
editor.putString(keyRow1, concatenateInfoRow1);
// commits your edits
editor.commit();
} // end if initial edittext is empty
// --------------------------------------------------------------------------
textViewProteinRow1.setText(textInputRow1ResultProtein);
textViewCarbsRow1.setText(textInputRow1ResultCarbs);
textViewFatRow1.setText(textInputRow1ResultFat);
textViewCaloriesRow1.setText(textInputRow1ResultCalories);
} // end if FRUITS
} // end for FRUITS
break;
} // END HAND SUBMMIT RED IMAGE BUTTON
else if (ct.closeMatch(Color.WHITE, touchColor, tolerance))
nextImage = R.drawable.background_food_tracker;
;
if (currentResource == nextImage) {
nextImage = R.drawable.background_food_tracker;
}
handledHere = true;
break;
default:
handledHere = false;
} // end switch
if (handledHere) {
if (nextImage > 0) {
imageView.setImageResource(nextImage);
imageView.setTag(nextImage);
}
}
return handledHere;
}// end
// Method
public int getHotspotColor(int hotspotId, int x, int y) {
ImageView img = (ImageView) findViewById(hotspotId);
if (img == null) {
// Log.d ("SubMain", "Hot spot image not found");
return 0;
} else {
img.setDrawingCacheEnabled(true);
Bitmap hotspots = Bitmap.createBitmap(img.getDrawingCache());
if (hotspots == null) {
// Log.d ("ImageAreasActivity", "Hot spot bitmap was not
// created");
return 0;
} else {
img.setDrawingCacheEnabled(false);
return hotspots.getPixel(x, y);
}
}
}// End Method
// ----------------------------------------------------------------------
public void tToast(String s) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, s, duration);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
// ------------------------------------------------------------------------------------------------------------------
public void onStart() {
super.onStart();
// GET INFORMATION BACK FROM SPINNER BASED ON POSITION STORED
sharedPrefGetInfo = PreferenceManager.getDefaultSharedPreferences(this);
int positionRow1 = sharedPrefGetInfo.getInt("ROW1", 0);
// tToast(String.valueOf(positionRow1)); //TESTING
if (positionRow1 >= 0) {
// LOAD SPINNER INFORMATION ROW1
spinnerFoodCategoryRow1.setSelection(positionRow1);
}
//---------------------------------------HERE IS THE PROBLEM SETTING THE CORRECT VALUE FOR SPINNER --------------------
//THE SPINNER 2 IS NOT BEING SET
Resources res = getResources();
oilsFoodInformation = res.getStringArray(R.array.SpinnerArrayInfoOils);
if (spinnerFoodCategoryRow1.getSelectedItemPosition() == positionRow1) {
tToast("OILS TYPE TRUE");
int positionRow1Oils = sharedPrefGetInfo.getInt("ROW1-OILS", 0);
//THIS IS THE CORRECT POSITION FOR SPINNER 2
tToast("POSITION FOODNAME BACK = " + String.valueOf(positionRow1Oils));
// validation
if (positionRow1Oils >= 0) {
for (int n = 0; n < oilsFoodInformation.length; n++) {
spinnerFoodNameRow1Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
res.getStringArray(R.array.SpinnerArrayInfoOils));
spinnerFoodNameRow1.setAdapter(spinnerFoodNameRow1Adapter);
tToast("Iteration # = " + n);
// LOAD SPINNER INFORMATION ROW1 OILS
if (n == positionRow1Oils) {
tToast("SPINNER FOOD NAME IS SET");
spinnerFoodNameRow1.setSelection(positionRow1Oils); // NOT
// WORKING
}
}
} // end if
}
// -----------------------------------------------------------------------------------------------------------------------
currentDateString = DateFormat.getDateInstance().format(new Date());
keyRow1 = currentDateString + "-" + "keyRow1";
getBackInformationRow1 = sharedPrefGetInfo.getString(keyRow1, "");
// MAKE SURE THE ARRAY KEY IS NOT EMPTY
if (!getBackInformationRow1.equals("")) {
getBackInformationParseRow1 = getBackInformationRow1.split(" - ", 4);
textViewProteinRow1.setText(getBackInformationParseRow1[0]);
textViewCarbsRow1.setText(getBackInformationParseRow1[1]);
textViewFatRow1.setText(getBackInformationParseRow1[2]);
textViewCaloriesRow1.setText(getBackInformationParseRow1[3]);
}
}// end class