数据未传输到新 activity
Data not transferring to a new activity
我做错了什么?我无法将我的数据从我在列表中单击它的位置传输到单击打开的详细视图 activity。
这是我在单击按钮时的代码...由于列表显示了项目,因此数据可在此处获得。
RecipeRepo repo = new RecipeRepo(this);
final ArrayList<HashMap<String, String>> recipeList = repo.getRecipeList();
if(recipeList.size()!=0) {
ListView lv = (ListView) findViewById(R.id.list);//getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
recipe_Id = (TextView) view.findViewById(R.id.recipe_Id);
String recipeId = recipe_Id.getText().toString();
Intent objIndent = new Intent(getApplicationContext(),RecipeDetail.class);
objIndent.putExtra("recipe_Id", Integer.parseInt( recipeId));
startActivity(objIndent);
}
});
ListAdapter adapter = new SimpleAdapter( SousChef.this,recipeList, R.layout.view_recipe_entry, new String[] { "id","name"}, new int[] {R.id.recipe_Id, R.id.recipe_list_name});
lv.setAdapter(adapter);
}else {
Toast.makeText(this, "No recipe!", Toast.LENGTH_SHORT).show();
}
就像我说的,我现在可以使用数据,因为列表视图显示了每个项目的 ID 和名称。
在我使用以下代码点击它之后...activity 是空白的,下面项目中的 toast 出现 ID 0
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_detail);
btnEdit = (Button) findViewById(R.id.detail_edit);
btnClose = (Button) findViewById(R.id.detail_close);
textName = (EditText) findViewById(R.id.detail_recipe_name);
textIngredients = (EditText) findViewById(R.id.detail_recipe_ingredients);
textInstruct = (EditText) findViewById(R.id.detail_recipe_instruct);
textCookTemp = (EditText) findViewById(R.id.detail_cook_temp);
textCookTime = (EditText) findViewById(R.id.detail_recipe_cooktime);
textServes = (EditText) findViewById(R.id.detail_recipe_servings);
btnEdit.setOnClickListener(this);
btnClose.setOnClickListener(this);
_Recipe_Id =0;
Intent intent = getIntent();
_Recipe_Id = intent.getIntExtra("recipe_Id", 0);
RecipeRepo repo = new RecipeRepo(this);
Recipe recipe = new Recipe();
recipe = repo.getRecipeById(_Recipe_Id);
textName.setText(recipe.name);
Toast.makeText(this, "Recipe id is " + recipe.recipe_Id, Toast.LENGTH_SHORT).show();
textIngredients.setText(recipe.ingredients);
textInstruct.setText(recipe.instructions);
textCookTemp.setText(recipe.cooktemp);
textCookTime.setText(recipe.cooktime);
textServes.setText(recipe.serves);
从我读过的所有内容来看,这应该是可行的,但我必须遗漏一些东西。我也没有在 logCat 或任何东西中产生任何错误。
忠告...始终确保您在数据库中标记正确的列...一个简单的错误可能会导致您花费 2 天的时间完成 window。在这种情况下,我使用 getRecipeByID 代码查找列类别而不是列 recipe_Id。简单的修复,但本可以避免。
我做错了什么?我无法将我的数据从我在列表中单击它的位置传输到单击打开的详细视图 activity。
这是我在单击按钮时的代码...由于列表显示了项目,因此数据可在此处获得。
RecipeRepo repo = new RecipeRepo(this);
final ArrayList<HashMap<String, String>> recipeList = repo.getRecipeList();
if(recipeList.size()!=0) {
ListView lv = (ListView) findViewById(R.id.list);//getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
recipe_Id = (TextView) view.findViewById(R.id.recipe_Id);
String recipeId = recipe_Id.getText().toString();
Intent objIndent = new Intent(getApplicationContext(),RecipeDetail.class);
objIndent.putExtra("recipe_Id", Integer.parseInt( recipeId));
startActivity(objIndent);
}
});
ListAdapter adapter = new SimpleAdapter( SousChef.this,recipeList, R.layout.view_recipe_entry, new String[] { "id","name"}, new int[] {R.id.recipe_Id, R.id.recipe_list_name});
lv.setAdapter(adapter);
}else {
Toast.makeText(this, "No recipe!", Toast.LENGTH_SHORT).show();
}
就像我说的,我现在可以使用数据,因为列表视图显示了每个项目的 ID 和名称。
在我使用以下代码点击它之后...activity 是空白的,下面项目中的 toast 出现 ID 0
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_detail);
btnEdit = (Button) findViewById(R.id.detail_edit);
btnClose = (Button) findViewById(R.id.detail_close);
textName = (EditText) findViewById(R.id.detail_recipe_name);
textIngredients = (EditText) findViewById(R.id.detail_recipe_ingredients);
textInstruct = (EditText) findViewById(R.id.detail_recipe_instruct);
textCookTemp = (EditText) findViewById(R.id.detail_cook_temp);
textCookTime = (EditText) findViewById(R.id.detail_recipe_cooktime);
textServes = (EditText) findViewById(R.id.detail_recipe_servings);
btnEdit.setOnClickListener(this);
btnClose.setOnClickListener(this);
_Recipe_Id =0;
Intent intent = getIntent();
_Recipe_Id = intent.getIntExtra("recipe_Id", 0);
RecipeRepo repo = new RecipeRepo(this);
Recipe recipe = new Recipe();
recipe = repo.getRecipeById(_Recipe_Id);
textName.setText(recipe.name);
Toast.makeText(this, "Recipe id is " + recipe.recipe_Id, Toast.LENGTH_SHORT).show();
textIngredients.setText(recipe.ingredients);
textInstruct.setText(recipe.instructions);
textCookTemp.setText(recipe.cooktemp);
textCookTime.setText(recipe.cooktime);
textServes.setText(recipe.serves);
从我读过的所有内容来看,这应该是可行的,但我必须遗漏一些东西。我也没有在 logCat 或任何东西中产生任何错误。
忠告...始终确保您在数据库中标记正确的列...一个简单的错误可能会导致您花费 2 天的时间完成 window。在这种情况下,我使用 getRecipeByID 代码查找列类别而不是列 recipe_Id。简单的修复,但本可以避免。