2016-04-05 8 views
1

私はどのように表示したいのかをカスタマイズしたAlertDialogを継承するクラスFoodDialogを持っています。AlertDialog.Builderを使用してカスタムAlertDialogクラスを構築する

私は今AlertDialog.Builderを使用して、正/負のボタンを編集したいのです、私はビルダーを使用してFoodDialogのインスタンスを構築しようとすると、しかし、私はビルダーがAlertDialogを求めている「互換性のないタイプ」というエラーが直面しています代わりにAlertDialogという拡張子を付けています - これはどういう意味ですか?

私のカスタムAlertDialogクラスFoodDialogの肯定/否定ボタンを編集する方法はありますか?

以下は私のFoodDialogクラスです。私が持っているはい/いいえのボタンは自分で作ったものですが、柔らかいキーボードが現れると、これらのボタンが見えなくなるので代わりにAlertDialog.Builderの一部が表示されます:

public class FoodDialog extends AlertDialog implements OnClickListener { 

    private TextView foodNameTextView, foodDescTextView, foodPortionTextView, catTextView, qtyText, cal, fat, sFat, carb, sug, prot, salt, imageTxt, 
      measureText; 
    private EditText foodQty; 
    private ImageView foodImage; 
    private ImageButton yesBtn, noBtn; 
    private int foodID, totalCal; 
    private Bitmap image; 
    private String user, portionType, foodName, foodDesc, cat, totalCalString, totalFatString, 
      totalSFatString, totalCarbString, totalSugString, totalProtString, totalSaltString, portionBaseString; 
    private double totalFat, totalSFat, totalCarb, totalSug, totalProt, totalSalt, portionBase; 
    private Food food; 
    private Portion portion; 
    private Nutrients nutrients; 
    private PortionType pType; 
    private DBHandler db; 

    public FoodDialog(Context context){ 
     super(context); 

    } 

    public FoodDialog(Context context, int foodID, String imgLocation, final String user) { 
     super(context, android.R.style.Theme_Holo_Light_Dialog); 
     this.setTitle("Confirm?"); 
     setContentView(R.layout.dialog_layout); 
     this.foodID = foodID; 
     this.user = user; 

     db = new DBHandler(context); 
     food = db.getFoodByID(foodID, user); 
     portion = db.getPortionByFoodID(foodID); 
     nutrients = db.getNutrientsByFoodIDAndPortionType(foodID, portion.getPortionType()); 
     pType = db.getPortionTypeByName(portion.getPortionType()); 

     //getting object attributes 
     portionType = portion.getPortionType(); 
     portionBase = portion.getPortionBase(); 

     //food 
     foodName = food.getName(); 
     foodDesc = food.getDesc(); 
     cat = food.getCat(); 

     //nutrients 
     totalCal = nutrients.getCal(); 
     totalFat = nutrients.getFat(); 
     totalSFat = nutrients.getSFat(); 
     totalCarb = nutrients.getCarb(); 
     totalSug = nutrients.getSug(); 
     totalProt = nutrients.getProt(); 
     totalSalt = nutrients.getSalt(); 

     //converting to string 
     totalCalString = String.valueOf(totalCal); 

     if (totalFat % 1 == 0) { 
      totalFatString = String.format("%.0f", totalFat); 
     } else { 
      totalFatString = String.valueOf(totalFat); 
     } 

     if (totalSFat % 1 == 0) { 
      totalSFatString = String.format("%.0f", totalSFat); 
     } else { 
      totalSFatString = String.valueOf(totalSFat); 
     } 

     if (totalCarb % 1 == 0) { 
      totalCarbString = String.format("%.0f", totalCarb); 
     } else { 
      totalCarbString = String.valueOf(totalCarb); 
     } 

     if (totalSug % 1 == 0) { 
      totalSugString = String.format("%.0f", totalSug); 
     } else { 
      totalSugString = String.valueOf(totalSug); 
     } 

     if (totalProt % 1 == 0) { 
      totalProtString = String.format("%.0f", totalProt); 
     } else { 
      totalProtString = String.valueOf(totalProt); 
     } 

     if (totalSalt % 1 == 0) { 
      totalSaltString = String.format("%.0f", totalSalt); 
     } else { 
      totalSaltString = String.valueOf(totalSalt); 
     } 

     if (portionBase % 1 == 0) { 
      portionBaseString = String.format("%.0f", portionBase); 
     } else { 
      portionBaseString = String.valueOf(portionBase); 
     } 

     //textviews 
     foodNameTextView = (TextView) findViewById(R.id.dialogName); 
     foodNameTextView.setText(foodName); 
     foodDescTextView = (TextView) findViewById(R.id.dialogDesc); 
     foodDescTextView.setText(foodDesc); 
     foodPortionTextView = (TextView) findViewById(R.id.dialogPortion); 
     foodPortionTextView.setText("Values based per " + portionBase + " " + portionType); 
     catTextView = (TextView) findViewById(R.id.dialogCat); 
     catTextView.setText(cat); 
     measureText = (TextView) findViewById(R.id.dialogMeasure); 
     measureText.setText(portionType); 
     qtyText = (TextView) findViewById(R.id.dialogQtyText); 
     imageTxt = (TextView) findViewById(R.id.dialogImageText); 
     cal = (TextView) findViewById(R.id.dialogCal); 
     cal.setText(totalCalString); 
     fat = (TextView) findViewById(R.id.dialogFat); 
     fat.setText(totalFatString + "g"); 
     sFat = (TextView) findViewById(R.id.dialogSFat); 
     sFat.setText(totalSFatString + "g"); 
     carb = (TextView) findViewById(R.id.dialogCarb); 
     carb.setText(totalCarbString + "g"); 
     sug = (TextView) findViewById(R.id.dialogSug); 
     sug.setText(totalSugString + "g"); 
     prot = (TextView) findViewById(R.id.dialogProt); 
     prot.setText(totalProtString + "g"); 
     salt = (TextView) findViewById(R.id.dialogSalt); 
     salt.setText(totalSaltString + "g"); 

     //img 
     foodImage = (ImageView) findViewById(R.id.dialogImage); 
     imgLocation = food.getImgURL(); 
     image = BitmapFactory.decodeFile(imgLocation); 
     foodImage.setImageBitmap(image); 

     if (imgLocation.equals("nourl")) { 
      imageTxt.setText("No Image"); 
     } 

     //edit tex 
     foodQty = (EditText) findViewById(R.id.dialogQty); 

     //adjusting edittext 
     foodQty.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); 
     foodQty.setFilters(new InputFilter[]{ 
       new DigitsKeyListener(Boolean.FALSE, Boolean.TRUE) { 
        int beforeDecimal = 4, afterDecimal = 3; 

        @Override 
        public CharSequence filter(CharSequence source, int start, int end, 
               Spanned dest, int dstart, int dend) { 
         String temp = foodQty.getText() + source.toString(); 

         if (temp.equals(".")) { 
          return "0."; 
         } else if (temp.toString().indexOf(".") == -1) { 
          // no decimal point placed yet 
          if (temp.length() > beforeDecimal) { 
           return ""; 
          } 
         } else { 
          temp = temp.substring(temp.indexOf(".") + 1); 
          if (temp.length() > afterDecimal) { 
           return ""; 
          } 
         } 
         return super.filter(source, start, end, dest, dstart, dend); 
        } 
       } 
     }); 
     foodQty.setText(portionBaseString); 

     //btns 
     yesBtn = (ImageButton) findViewById(R.id.yesBtn); 
     noBtn = (ImageButton) findViewById(R.id.noBtn); 

     Bitmap tick = BitmapFactory.decodeResource(context.getResources(), 
       R.drawable.png_tick); 

     Bitmap cross = BitmapFactory.decodeResource(context.getResources(), 
       R.drawable.png_cross); 

     yesBtn.setImageBitmap(tick); 
     noBtn.setImageBitmap(cross); 

     yesBtn.setOnClickListener(this); 
     noBtn.setOnClickListener(this); 

     getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
    } 


    @Override 
    public void onClick(View v) { 

     if (v == yesBtn) { 
      SimpleDateFormat currentDate = new SimpleDateFormat("yyyy-MM-dd"); 
      SimpleDateFormat currentTime = new SimpleDateFormat("HH:mm:ss"); 
      String date = currentDate.format(new Date()); 
      String time = currentTime.format(new Date()); 

      double qty = 0; 
      //get quantity amount 
      // if (portionMeasure.equals("singles")) { 
      //qty = foodQty.getValue(); 
      // } else { 
      if (foodQty.getText().length() != 0) { 
       qty = Double.valueOf(foodQty.getText().toString()); 
      } else { 
       qty = 0; 
      } 
      // } 
      if (qty == 0 || String.valueOf(qty) == "") { 

       Toast.makeText(getContext(), "Please enter an amount", Toast.LENGTH_SHORT).show(); 
      } else { 
       //create new intake 
       Intake intake = new Intake(0, foodID, portionType, qty, date, time); 
       //record it and increment food used value 
       db.recordIntake(intake, user); 
       db.incrementUsedCount(intake.getFoodID(), 1); 
       db.close(); 
       cancel(); 
       Toast.makeText(getContext(), foodName + " recorded", Toast.LENGTH_SHORT).show(); 
       AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); 
       builder.setTitle("What next?"); 
       builder.setItems(new CharSequence[] 
           {"Record another food intake..", "Main Menu..", "View Stats.."}, 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           // The 'which' argument contains the index position 
           // of the selected item 
           switch (which) { 
            case 0: 
             cancel(); 
             break; 
            case 1: 
             Intent main = new Intent(getContext(), ProfileActivity.class); 
             getContext().startActivity(main); 
             break; 
            case 2: 
             Intent stats = new Intent(getContext(), StatsActivity.class); 
             getContext().startActivity(stats); 
             break; 
           } 
          } 
         }); 
       AlertDialog choose = builder.create(); 
       choose.show(); 
      } 

     } else if (v == noBtn) { 
      cancel(); 
     } 

    } 

} 
+0

テキストを編集したい(テキストを変更しますか?) –

+0

肯定と否定のボタン(テキストとリスナー)を編集できるようにします。現時点では、表示されるものはありません。 – blueprintChris

+1

投稿を更新してFoodDialogクラスを追加できますか? –

答えて

0

次のようにリスナーをクリックして、ボタンをキャッチすることができます:

yesBtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //yes button click code here 
     } 
    }); 

noBtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //no button click code here 
     } 
    }); 

あなたのリスナーが発射されているかどうかを確認するためにlogcatを使用することができます。

+0

私はこれをカスタムクラスの内側か外か?私が提供したコードを使って私に例を挙げてもらえますか? ありがとう – blueprintChris

+0

foodNameTextViewの取得方法を確認しますか?それをちょうど好きにしますが、ここで指定したコードで行います。さらに助けが必要な場合は、私に知らせてください。 –

+0

が動作しません - これはヌルオブジェクトを返すことによりonClickを設定できません '仮想メソッドを呼び出そうとしました 'voidヌードオブジェクトリファレンスで' android.widget.Button.setOnClickListener(android.view.View.View $ OnClickListener) 'を実行しました – blueprintChris

関連する問題