我无法让我的Android应用在TextViews中显示正确的信息。添加了Toasts以确保变量正好传递给kw1,kw2和kw3。对象'用户'传递正常,我可以用setText手动设置TextView("示例")但是我想使用以下代码将TextView数组中的文本更改为我的searchSuggest()方法返回的结果:
for (int index3 = 0; index3 < validRecipe; index3++){
recipeText[index3].setText((SetRecipes.recipes[temp[index3]].getName()));
}
SetRecipes是硬编码配方的Java类的名称 - suggestMethod()是在其中实现的,直到我无法使其工作,只是在这里复制代码。 (这解释了为什么该方法不再被称为任何东西,并且在OnCreate()中)。
有谁知道为什么TextViews中的文字没有改变?
很抱歉,但是我已经坐了好几个小时谷歌搜索,无法找到明确的答案。我是一个相对较新的编程和Android的初学者,所以它可能是与Java活动连接Java类非常基本的东西。如果您需要任何其他代码,请与我们联系。
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);
kw2 = user1.getDislikes(1);
kw3 = user1.getDislikes(2);
recipeText[0] = (TextView)findViewById(R.id.recipeText1);
recipeText[1] = (TextView)findViewById(R.id.recipeText2);
recipeText[2] = (TextView)findViewById(R.id.recipeText3);
final int MAXRECIPES = 6;
int[] temp = new int[MAXRECIPES];
int validRecipe = 0;
SetRecipes.setArray();
for (int index = 0; index < MAXRECIPES; index++)
{
if (!(SetRecipes.recipes[index].searchkeywords1(kw1, kw2, kw3)))
{
temp[validRecipe] = index;
validRecipe++;
} //if
}
if (validRecipe == 0)
{
Context context = getApplicationContext();
CharSequence text2 = "No valid recipes found!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text2, duration);
toast.show();
}
for (int index3 = 0; index3 < validRecipe; index3++)
{
recipeText[index3].setText((SetRecipes.recipes[temp[index3]].getName()));
}
Context context = getApplicationContext();
CharSequence text2 = kw1;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text2, duration);
toast.show();
}