2017-12-09 7 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"); 
      } 
     } 
    }); 
} 

가 간다 클래스 :

자신의 튜토리얼에서는
//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); 

, 잘 작동하지만 난 후 그것을하려고 할 때 일치하는 항목을 편집, 내 로그는 MatchId = -1 때 함께 제공 그것은해서는 안됩니다. 당신이 통과 한

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

에 코드를 바꿉니다. Java는 대소 문자를 구분합니다.

0

의 핵심은 MatchId이며 에서 동일하지 matchId을, 데이터를 가져 오는 있습니다 :

는 또한 데이터베이스의 방법을 포함합니다.

당신은 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); 

에게 잡 활동 변경에

selectedID = receivedIntent.getIntExtra("MatchId", -1); //NOTE: -1 is just the default value