2016-04-09 15 views
0

Bir işlevi çağırmak için bir düğme almaya çalışıyorum. Tek sorun, işlev, başka bir işlevden bir parametre çağırır ve bir hata alıyorum: AddRecipe içinde SetRecipe (int)() için uygulanamaz. Doğru terminoloji ne olursa olsun ne demek istediğimi/aramayı kastettiğim hakkında hiçbir fikrim yok.İşlevleri geçirme ve sonra da işlev çağırma düğmesiTıklama

Button saveRecipe = (Button) findViewById(R.id.btnAddRecipe); 
    saveRecipe.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      SetRecipe(); 
     } 
    }); 

Ve görevleri şunlardır:

düğme kodudur

public int getDifficulty (View v) 
{ 
    int Difficulty = -1; 
    boolean checked = ((RadioButton) v).isChecked(); 
    switch(v.getId()) { 
     case R.id.btnDiff1: 
      if (checked) 
       Difficulty = 1; 
      break; 
     case R.id.btnDiff2: 
      if (checked) 
       Difficulty = 2; 
      break; 
     case R.id.btnDiff3: 
      if (checked) 
       Difficulty = 3; 
      break; 
     case R.id.btnDiff4: 
      if (checked) 
       Difficulty = 4; 
      break; 
     case R.id.btnDiff5: 
      if (checked) 
       Difficulty = 5; 
      break; 
    } 

    return Difficulty; 
} 

public void SetRecipe (int Difficulty) 
{ 
    TextView RecipeNameView = (TextView) findViewById(R.id.editRecipeName); 
    TextView CookTimeView = (TextView) findViewById(R.id.editCookTime); 
    Spinner MealTypeView = (Spinner) findViewById(R.id.editMeal); 
    TextView HowToView = (TextView) findViewById(R.id.editHowTo); 

    String RecipeName = RecipeNameView.getText().toString(); 
    int CookTime = Integer.parseInt(CookTimeView.getText().toString()); 
    String MealType = MealTypeView.getSelectedItem().toString(); 
    String HowTo = HowToView.getText().toString(); 


    DatabaseHandler dbh = new DatabaseHandler(this); 
    dbh.SetRecipe(RecipeName, CookTime, Difficulty, MealType, HowTo); 

} 

ben vermek istiyorum Tüm getDifficulty işlevinden int Zorluğu almak ve SetRecipe iletecek olan işlevi. SetRecipe işlevi, kullanıcı bir düğmeye bastığında çağrılır. Eğer bu aptalca bir hata ise, özür dilerim, kodlamada çok yeni ve bu yüzden ne yaptığımı gerçekten bilmiyorum. Herhangi bir yardım büyük takdir edilecektir!

cevap

0

Difficulty = 1, 2, 3,3 demek yerine, onu doğrudan arayabilir misiniz? Ben View.OnClickListener ... uyguladıklarında

SetRecipe(1); 

Ayrıca ...

public class yourclass extends AppCompatActivity implements View.OnClickListener { 
Button setrecipe1; 
Button setrecipe2; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.yourlayout); 

    setrecipe1= (Button) findViewById(R.id.setrecipe1); 
    setrecipe2= (Button) findViewById(R.id.setrecipe2); 
    setrecipe1.setOnClickListener(this); 
    setrecipe2.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 
    switch(v.getId()){ 
     case R.id.setrecipe1: 
      SetRecipe(1); 
      break; 
     case R.id.setrecipe2: 
      SetRecipe(2); 
      break; 
     default: 
      break; 
} 
} 
} 
+0

Ne denetimi gelen zorluk tamsayı alıyoruz? –

0

deneyin OnClickListener btnAddRecipe etiketine android:onClick="getDifficulty" özellik ekleyerek ve çıkarın ...
Bu

çalışmalıdır Sizin OnCreate önce
0

yöntem beyan:

int Difficulty; 
senin getDifficulty yöntemde

kaldırmak int:

Tıklama düğmenin içinde daha sonra
int Difficulty = -1; ==> Difficulty = -1; 

:

Button saveRecipe = (Button) findViewById(R.id.btnAddRecipe); 
saveRecipe.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     SetRecipe(Difficulty); 
    } 
}); 

İyi şanslar

+0

Merhaba! Bunu denedim ancak veritabanını ADM'den aldığımda açtığımda, seçtiğim zorluk ne olursa olsun 0, veritabanına yerleştirilen değerdir. – alexpagee

+0

hata ya da 'NullPointerExcpetion' dan nereden geldiğini hiçbir fikrim yok? .. OnCreate yönteminden önce bildirmek için deneyin: int Difficulty = 1;' ve bana bildirin –

+0

Evet, şu anda 1 olarak saklanıyor. Zorluğun değeri geçmiyor ama neden olduğunu anlayamıyorum :( – alexpagee

İlgili konular