2016-04-09 10 views
0

関数を呼び出すためのボタンを取得しようとしています。唯一の問題は、関数が別の関数からパラメータを呼び出し、エラーが発生していることです。AddRecipeのSetRecipe(int)を()に適用できません。正しい用語が何であっても、私は何を渡す/呼ぶべきか分かりません。関数内のパラメータを渡してから関数onClickを呼び出します。

ボタンのコードは次のとおりです。

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

と機能は次のとおりです。

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); 

} 

私が何をしているよすべてがgetDifficulty関数からint型の難しさを取得し、SetRecipeにそれを渡しています関数。ユーザーがボタンを押すと、SetRecipe関数が呼び出されます。

愚かな間違いであれば、私は謝罪します。私はコーディングが非常に新しいので、自分が何をしているのか分かりません。どんな助けでも大歓迎です!

答えて

0

難易度= 1,2,3,4と言うのではなく、直接呼び出すことはできますか?

SetRecipe(1); 
私はView.OnClickListenerを実装しても

... ... btnAddRecipeタグにandroid:onClick="getDifficulty"属性を追加

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

あなたから難易度の整数をどうコントロールを得ていますか? –

0

試してみて、onClickListenerを削除...
これは

を動作するはずです
0

あなたのOnCreateメソッドが宣言される前に:

intを削除し、あなたのgetDifficulty方法で:

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); 
    } 
}); 

幸運

+0

こんにちは!私はこれを試しましたが、ADMからデータベースを取り出して開くとき、0は私が選択した難易度に関係なく、データベースに格納される値です。私はどこから値0を取得しているのかわかりません... – alexpagee

+0

エラーまたは 'NullPointerExcpetion'はありませんか?あなたのOnCreateメソッドの前に宣言しようとしてください: ' int Difficulty = 1; ' –

+0

ええ、今は1として保存されています。難易度の値は渡されていませんが、私は理由を理解できません:( – alexpagee