数据未转移到新活动

时间:2017-10-12 02:23:05

标签: java android database sqlite data-transfer

有人可以解释一下我做错了什么吗?我无法将我的数据从我在列表中单击的位置转移到点击打开的详细视图活动。

以下是按钮点击时的代码...由于列表显示了项目,因此可以在此处获取数据。

            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和名称。

使用以下代码点击后...活动为空白,下面项目中的吐司出现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或其他任何内容中产生任何错误。

1 个答案:

答案 0 :(得分:0)

建议之词...始终确保在数据库中标记正确的列...一个简单的错误可能会导致您需要2天的工作时间。在这种情况下,我使用getRecipeByID代码查找列类别而不是列recipe_Id。简单的修复,但可以避免。