2017-10-12 59 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。簡單的修復,但可以避免。