1

我正在尝试传递在我的项目的这一部分中设置的变量“stockSymbol”...

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

4 回答 4

3

这是一个常见的范围问题。您在方法中声明了一个新StockSymbol字符串onClick。要解决此问题,只需更改行

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

stockSymbol = stock.getText().toString();
于 2013-08-21T19:20:26.063 回答
3

在 onClick 方法中,您将 stockSymbol 声明为局部变量。不要那样做,而是使用在父类中定义的 stockSymbol。

于 2013-08-21T19:06:58.430 回答
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();
于 2013-08-21T19:13:38.507 回答
0

似乎仍然没有采用从我的文本视图传递的变量。这是从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;
于 2013-08-21T20:40:09.223 回答