2017-10-12 56 views
1

有人能解释我做错了什么吗?我无法将我的数据从列表中单击它的位置转移到点击打开的详细查看活动。未将数据传输到新活动

这里是我点击按钮时的代码...并且由于列表显示项目,因此数据可用。

  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(); 
     } 

就像我说的,我现在有数据可用,因为listview显示了每个项目的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或任何东西中产生任何错误。

+0

经过多一点研究后,我发现_Recipe_Id正在读取正确的数字,但未传输至配方。因此它给我的recipe.recipe_id数字0.想法? – Koumintang

+1

确定问题出在我的getRecipeById代码中,从哪个列读取...问题的一个简单错误是通过将其更改为从ID列中读取来修复的 – Koumintang

+0

如果问题已解决,您可以添加自己的答案并接受它。 –

回答

0

建议的话...总是确保你在数据库中标记正确的列...一个简单的错误可能会导致你2天的工作去窗外。在这种情况下,我有getRecipeByID代码寻找列类别,而不是列recipe_Id。简单的修复,但可以避免。