如何在我的onClick函数中设置findViewById是正确的

时间:2012-12-05 19:31:06

标签: java android android-layout

我的“findViewById”出现了问题,它出现在该行之间。

 new AlertDialog.Builder(DetailShareHoldingActivity.this).setTitle("Köp " + Portfolio.getPortfolio().getShareHolding(positionList).getName() +  " Aktier").setView(addView).setPositiveButton("Köp", new DialogInterface.OnClickListener() {

和行

public void onClick(DialogInterface dialog, int whichButton) {

如果我把它放在任何其他工作正常的地方,问题是我必须准确地说明我所拥有的那条线,所以我不知道它为什么抱怨? 这是我的OnclickListener

buyButton = (Button)findViewById(R.id.buyButton);
    buyButton.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
        EditText tempEditText;    
        final View addView = getLayoutInflater().inflate(R.layout.shop_exsisting_share, null);
        new AlertDialog.Builder(DetailShareHoldingActivity.this).setTitle("Köp " + Portfolio.getPortfolio().getShareHolding(positionList).getName() +  " Aktier").setView(addView).setPositiveButton("Köp", new DialogInterface.OnClickListener() {
            tempEditText = (EditText)findViewById(R.id.nrOfSharesInPortfolioEdit);//Complains on findViewById "Return type for the method is missing" and I have to have the code exactly here so that it is visible when the dialog is created and not when the button is pressed.
            tempEditText= (EditText) findViewById(R.id.nrOfSharesInPortfolioEdit);
            tempEditText.setText(Portfolio.getPortfolio().getShareHolding(positionList).getNrOfSharesInPortfolio());
            System.out.println(Portfolio.getPortfolio().getShareHolding(positionList).getNrOfSharesInPortfolio());
            tempEditText= (EditText) findViewById(R.id.currentCourseEdit);
            tempEditText.setText(Double.toString(Portfolio.getPortfolio().getShareHolding(positionList).getCurrentRate()));

            public void onClick(DialogInterface dialog, int whichButton) {
                System.out.println("Button pressed köp ");
            }
        }).setNegativeButton("Avbryt", null).show();
        }
    });

1 个答案:

答案 0 :(得分:2)

是的,当然你遇到了问题。您正试图在类定义中定义已声明的变量...这是不允许的。

您的评论说,“我必须准确地将代码放在此处,以便在创建对话框时可见,而不是按下按钮时。”。这没有任何意义;你需要在实际方法中使用你的代码。

将定义移出类定义之外,改为方法。

tempEditText = (EditText) addView.findViewById(R.id.nrOfSharesInPortfolioEdit);
tempEditText = (EditText) addView.findViewById(R.id.nrOfSharesInPortfolioEdit);
tempEditText.setText(Portfolio.getPortfolio().getShareHolding(positionList).getNrOfSharesInPortfolio());
System.out.println(Portfolio.getPortfolio().getShareHolding(positionList).getNrOfSharesInPortfolio());
tempEditText = (EditText) addView.findViewById(R.id.currentCourseEdit);
tempEditText.setText(Double.toString(Portfolio.getPortfolio().getShareHolding(positionList).getCurrentRate()));

new AlertDialog.Builder(DetailShareHoldingActivity.this)
    .setTitle("Köp " + Portfolio.getPortfolio().getShareHolding(positionList).getName() +  " Aktier")
    .setView(addView)
    .setPositiveButton("Köp", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            System.out.println("Button pressed köp ");
        }
    })
    .setNegativeButton("Avbryt", null)
    .show();