0

我正在为 Android 开发一个简单的食谱持有者应用程序。我是 Android 编程新手。我有两个活动——第一个活动是检查并输入搜索变量,第二个活动是 ListView。可以使用以下方法通过名称、难度和菜肴类型的值来搜索食谱:

public class DatabaseManager
{
 // other class stuff
 public Cursor getMatchingRecipes(String name, String difficulty, String recipeType)
 {
  // this method has been tested and works fine, the query itself works fine
 }
}

我正在创建一个启动 ListView 活动的意图,如下所示:

//code from the SearchParametersActivity
Intent intent = new Intent(this, RecipesListActivity.class);
Bundle bundle = new Bundle();
bundle.putString("name", name); // name is a String variable with value from an EditText
bundle.putString("difficulty", difficulty.toString()); // from a Spinner
bundle.putString("type", type.toString()); // from a Spinner
bundle.putInt("action", DatabaseManager.GET_SEARCH);
intent.putExtras(bundle);
startActivity(intent);

cursor = dbManager.getMatchingRecipes(name, difficulty, type); // calling this from this activity works fine and gives me the correct entries from the database

并从第二个活动中调用查询,如下所示:

//code from the RecipesListActivity
dbManager.open();
String name = getIntent().getExtras().getString("name");
String difficulty = getIntent().getExtras().getString("difficulty");
String type = getIntent().getExtras().getString("type");
cursor = dbManager.getMatchingRecipes(name, difficulty, type); // NOT WORKING

这个查询总是给 ma 一个空游标。我已经检查了字符串是否正确传递——它们在两个活动中都以相同的方式打印出来。但是,如果我手动输入相同的确切字符串值,我会得到正确的结果。

String name1 = "egg";
String difficulty1 = "EASY";
String type1 = "MAIN_COURSE";

cursor = dbManager.getMatchingRecipes(name1, difficulty1, type1); // WORKS

返回所有数据库行的类似方法在这两个活动中都可以正常工作,因此我在通过意图传递值时一定犯了一些错误。这让我一无所知,任何帮助将不胜感激。

4

1 回答 1

0

试试这个方法

Intent intent = new Intent(this, RecipesListActivity.class);
Bundle bundle = new Bundle();
bundle.putExtra("name", name); // name is a String variable with value from an EditText
bundle.putExtra("difficulty", difficulty.toString()); // from a Spinner
bundle.putExtra("type", type.toString()); // from a Spinner
bundle.putExtra("action", DatabaseManager.GET_SEARCH);

startActivity(intent);

//RecipesListActivity 中的代码

dbManager.open();
String name = getIntent().getStringExtra("name");
String difficulty = getIntent().getStringExtra("difficulty");
String type = getIntent().getStringExtra("type");
于 2013-09-09T08:52:26.410 回答