2017-12-09 13 views
0

YouTubeチュートリアルを視聴した後にインテントを使用してあるクラスから別のクラスにIDを渡そうとしています。しかし、それは常に私が望む値ではなく-1のデフォルト値を渡します。インテントでデータを渡す際にエラーが発生しました

mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
      String homeTeam = adapterView.getItemAtPosition(i).toString(); 
      Log.d(TAG, "onItemClick: You Clicked on " + homeTeam); 

      Cursor data = myDB.getMatch(homeTeam); //get the id associated with that name 
      int itemID = -1; 
      while(data.moveToNext()){ 
       itemID = data.getInt(0); 
      } 
      if(itemID > -1){ 
       //Log.d(TAG, "onItemClick: The ID is: " + itemID); 
       Intent editScreenIntent = new Intent(ViewMatchesActivity.this, UpdateMatchActivity.class); 
       editScreenIntent.putExtra("MatchId", itemID); 
       editScreenIntent.putExtra("homeTeam",homeTeam); 
       startActivity(editScreenIntent); 
      } 
      else{ 
       toastMessage("No ID associated with that name"); 
      } 
     } 
    }); 
} 

それはに行くクラス:=それが正常に動作しますが、私は試合を編集した後、私はそれをしようとすると、私のログにそれがMatchIdを思い付く-1とき、彼のチュートリアルで

//get the intent extra from the ListDataActivity 
    Intent receivedIntent = getIntent(); 

    //now get the itemID we passed as an extra 
    selectedID = receivedIntent.getIntExtra("matchId", -1); //NOTE: -1 is just the default value 

    //now get the name we passed as an extra 
    selectedName = receivedIntent.getStringExtra("homeTeam"); 

    //set the text to show the current selected name 
    editable_item.setText(selectedName); 

それはあってはならない。

私もデータベースに方法を含みます:

public void updateName(String newName, int id, String oldName){ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    String query = "UPDATE " + MATCH_TABLE + " SET " + MATCH_HOME_TEAM_COL + 
      " = '" + newName + "' WHERE " + MATCH_ID_COL + " = '" + id + "'" + 
      " AND " + MATCH_HOME_TEAM_COL + " = '" + oldName + "'"; 
    Log.d(TAG, "updateName: query: " + query); 
    Log.d(TAG, "updateName: Setting name to " + newName); 
    db.execSQL(query); 
} 

答えて

0

にあなたがmatchIDMatchId instaedを使用する必要があり、コード

//get the intent extra from the ListDataActivity 
    Intent receivedIntent = getIntent(); 

    //now get the itemID we passed as an extra 
    selectedID = receivedIntent.getIntExtra("MatchId", -1); //NOTE: -1 is just the default value 

    //now get the name we passed as an extra 
    selectedName = receivedIntent.getStringExtra("homeTeam"); 

    //set the text to show the current selected name 
    editable_item.setText(selectedName); 

をあなたのコードを交換してください。 Javaは大文字と小文字を区別します。

0

をお渡ししているキーは、はMatchIdであり、あなたは同じではありませんmatchId、からデータをフェッチしています。

Recieving活動の変更で

selectedID = receivedIntent.getIntExtra("MatchId", -1); //NOTE: -1 is just the default value 
関連する問題