2013-08-21 93 views
1

我試圖傳遞變量「stockSymbol」在我的項目,這部分設置使用...傳遞變量通過Android項目

ticker.setText("Stock Ticker is: " + stockSymbol); 

成爲我的項目的一部分相同的值。 ..

final String yqlURL = yahooURLFirst + stockSymbol + yahooURLSecond; 

下面是完整的代碼...

String stockSymbol = ""; 

...

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_stock_info); 

    stock = (EditText) findViewById(R.id.stock); 
    ticker = (TextView) findViewById(R.id.ticker); 
    btnquote = (Button) findViewById(R.id.btnquote); 
    btnquote.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      // get the name from edittext and storing into string variable 
      String stockSymbol = stock.getText().toString(); 

      // binding name to the textview 
      ticker.setText("Stock Ticker is: " + stockSymbol); 
     } 
    }); 

    // Initialize TextViews 
    companyNameTextView = (TextView) findViewById(R.id.companyNameTextView); 
    yearLowTextView = (TextView) findViewById(R.id.yearLowTextView); 
    yearHighTextView = (TextView) findViewById(R.id.yearHighTextView); 
    daysLowTextView = (TextView) findViewById(R.id.daysLowTextView); 
    daysHighTextView = (TextView) findViewById(R.id.daysHighTextView); 
    lastTradePriceOnlyTextView = (TextView) findViewById(R.id.lastTradePriceOnlyTextView); 
    changeTextView = (TextView) findViewById(R.id.changeTextView); 
    daysRangeTextView = (TextView) findViewById(R.id.daysRangeTextView); 

    // Sends a message to the LogCat 
    Log.d(TAG, "Before URL Creation " + stockSymbol); 

    // Create the YQL query 
    final String yqlURL = yahooURLFirst + stockSymbol + yahooURLSecond; 

回答

3

這是一個常見的範圍問題。您正在onClick方法中聲明新的StockSymbol字符串。爲了解決這個問題,只需將線

String stockSymbol = stock.getText().toString(); 

改變

stockSymbol = stock.getText().toString(); 
3

在onClick方法中,將stockSymbol聲明爲局部變量。不要那樣做,而應使用在父類中定義的stockSymbol。

1

或者如果你想去玩樂(?)解決方案。將變量設置爲視圖的標記值。

public void onClick(View v) { 
      // get the name from edittext and storing into string variable 
      String stockSymbol = stock.getText().toString(); 

      // binding name to the textview 
      ticker.setText("Stock Ticker is: " + stockSymbol); 
      ticker.setTag(stockSymbol); 
     } 

指定值。

final String yqlURL = ticker.getTag(); 
0

似乎仍不是採取從我的TextView傳遞的變量。以下是從OnClick偵聽器中刪除字符串的更完整版本的代碼。愛德華 - 我試圖實施你的「有趣」的解決方法,但無濟於事。

String stockSymbol = "D"; 

// Used to make the URL to call for XML data 
String yahooURLFirst = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quote%20where%20symbol%20in%20(%22"; 
String yahooURLSecond = "%22)&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"; 

EditText stock; 
Button btnquote; 
TextView ticker; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_stock_info); 

    stock = (EditText) findViewById(R.id.stock); 
    ticker = (TextView) findViewById(R.id.ticker); 
    btnquote = (Button) findViewById(R.id.btnquote); 
    btnquote.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      // get the name from edittext and storing into string variable 
      stockSymbol = stock.getText().toString(); 
      // binding name to the textview 
      ticker.setText(stockSymbol); 
     } 
    });  

    // Initialize TextViews 
    companyNameTextView = (TextView) findViewById(R.id.companyNameTextView); 
    yearLowTextView = (TextView) findViewById(R.id.yearLowTextView); 
    yearHighTextView = (TextView) findViewById(R.id.yearHighTextView); 
    daysLowTextView = (TextView) findViewById(R.id.daysLowTextView); 
    daysHighTextView = (TextView) findViewById(R.id.daysHighTextView); 
    lastTradePriceOnlyTextView = (TextView) findViewById(R.id.lastTradePriceOnlyTextView); 
    changeTextView = (TextView) findViewById(R.id.changeTextView); 
    daysRangeTextView = (TextView) findViewById(R.id.daysRangeTextView); 

    // Sends a message to the LogCat 
    Log.d(TAG, "Before URL Creation " + stockSymbol); 


    // Create the YQL query 
    final String yqlURL = yahooURLFirst + stockSymbol + yahooURLSecond; 
+1

這不是如何顯示有關您的問題的更多信息。你需要通過編輯你現有的問題來顯示更多,__uneditted__代碼(預問題)。此外,通過評論他們的答案告訴人們,這不起作用。 – Phil

+0

請不要用答案來回答其他答案。編輯原始帖子或使用評論 – Nick