我试图在项目的这一部分中传递变量“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;
答案 0 :(得分:3)
在onClick方法中,您将stockSymbol声明为局部变量。不要这样做,而是使用父类中定义的stockSymbol。
答案 1 :(得分:3)
这是一个常见的范围问题。您在StockSymbol
方法中声明了一个新的onClick
字符串。要解决此问题,只需更改行
String stockSymbol = stock.getText().toString();
到
stockSymbol = stock.getText().toString();
答案 2 :(得分: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();
答案 3 :(得分:0)
似乎还没有从我的textview传递变量。这是一个更完整的代码版本,其中从OnClick
侦听器中删除了String。爱德华 - 我试图实施你的“有趣”解决方法,但无济于事。
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;