android - 传递变量以通过 Android 项目使用

标签 android variables

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

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

对我项目的这一部分具有相同的值(value)...

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;

最佳答案

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

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

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

关于android - 传递变量以通过 Android 项目使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18365592/

相关文章:

python - 是否可以使用变量从特定对象调用方法?

java - 使用 jsp 将 javascript 变量添加到 session 属性

php - 从函数 PHP 内部更改全局变量

java - 如何从 ISO 8601 格式字符串中选择时区到日历实例中

java - 当应用程序被杀死然后重新启动时,Android 服务(在它自己的线程中)死亡

android - 加密 Realm 数据库

PHP变量初始化到数据库查询

java - 应用程序从未收到 GPS 修复

java - DrawerLayout onDrawerClosed() 调用两次

variables - 如何访问 .exe 运行 Wix Bootstrap 的文件夹?