我遇到了一個奇怪的問題。我試圖將一些表示用戶不喜歡的字符串變量傳遞給預定義的Java方法,該方法通過將這些不喜歡的內容與作爲String數組存儲在Recipe對象數組中的關鍵組件進行比較來工作。將字符串變量傳遞給Java方法不起作用,但硬編碼的字符串正在工作?
當我硬編碼厭惡,如「牛肉」,但當我使用user1.getDislikes(0)爲實例字符串變量kw1分配不喜歡的方法時,該方法工作正常 - 它返回具有「牛肉」作爲關鍵字的食譜,但不應該。
我知道字符串正在傳遞並正確分配,因爲我使用Toast在返回有效結果時顯示kw1。
我曾嘗試在許多地方添加toString(),因爲儘管聲明它是多餘的,但它在這裏沒有工作,因爲IntelliJ在早期對它進行挑剔。
這裏是我在遇到困難的部分:
if ((SetRecipes.recipes[index].searchkeywords2(kw1, kw2, kw3))) //Not working unless words (e.g. "Beef") are hardcoded for some reason. kw1 variable being assigned correctly, as shown by Toast.
{
temp[validRecipe] = index;
validRecipe++;
} //if
完整的代碼可以在下面找到。任何幫助是極大的讚賞!
public class SuggestResult extends Activity
{
String kw1, kw2, kw3;
static TextView [] recipeText = new TextView[8];
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.suggest_results);
User user1 = (User)getIntent().getSerializableExtra("user1");
kw1 = user1.getDislikes(0).toString();
kw2 = user1.getDislikes(1).toString();
kw3 = user1.getDislikes(2).toString();
/*
kw1 = "null";
kw2 = "null";
kw3 = "null";
*/
recipeText[0] = (TextView)findViewById(R.id.recipeSuggestText1);
recipeText[1] = (TextView)findViewById(R.id.recipeSuggestText2);
recipeText[2] = (TextView)findViewById(R.id.recipeSuggestText3);
recipeText[3] = (TextView)findViewById(R.id.recipeSuggestText4);
recipeText[4] = (TextView)findViewById(R.id.recipeSuggestText5);
recipeText[5] = (TextView)findViewById(R.id.recipeSuggestText6);
final int MAXRECIPES = 7;
final int MAXTEXTFIELDS = 6;
int[] temp = new int[MAXRECIPES];
int validRecipe = 0;
SetRecipes.setArray();
for (int index = 0; index < MAXRECIPES; index++)
{
if ((SetRecipes.recipes[index].searchkeywords2(kw1, kw2, kw3))) //Not working unless words (e.g. "Beef") are hardcoded for some reason. kw1 variable being assigned correctly, as shown by Toast.
{
temp[validRecipe] = index;
validRecipe++;
} //if
}
if (validRecipe == 0)
{
Context context = getApplicationContext();
CharSequence text = "No valid recipes found!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
for (int index3 = 0; (index3 < validRecipe) && (index3 < MAXTEXTFIELDS); index3++)
{
recipeText[index3].setText((SetRecipes.recipes[temp[index3]].getName()).toString());
}
Context context = getApplicationContext();
CharSequence text2 = kw1;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text2, duration);
toast.show();
}
}
searchkeywords2方法:
public boolean searchkeywords2(String choice1,String choice2, String choice3)
{
int ingredientsPresent = 0;
for (int index = 0; index < keywords.length; index++)
{
if ((keywords[index] == choice1) || (keywords[index] == choice2) || (keywords[index] == choice3))
{
ingredientsPresent++;
}
}
if (ingredientsPresent == 0)
{
return true;
} else
{
return false;
}
}
「不工作」......有什麼機會變得更具體? – Tom 2014-12-07 01:41:51
道歉,我已經更新瞭解釋。當我傳遞「牛肉」作爲參數時,該方法正在過濾包含關鍵字「牛肉」的食譜,但是當我將kw1作爲參數傳遞時,不會用牛肉過濾食譜。 – 2014-12-07 01:44:46
那麼有趣的方法是'searchkeywords2'?你可以把它添加到問題? – Tom 2014-12-07 01:45:49