繁体   English   中英

数据未传输到新活动

[英]Data not transferring to a new 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和名称。

使用以下代码单击它之后,该活动为空白,并且下面项目中的吐司显示为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天的时间才能完成工作。 在这种情况下,我使用了getRecipeByID代码来查找列类别而不是列recipe_Id。 简单修复,但可以避免。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM