我正在尝试编写一个货币转换器,我想从雅虎财经获取信息,然后将其保存到数据库中。到目前为止,一切都工作正常,除了一件事是我无法访问 rates[k] 的值,我可以访问 rates[k] 的唯一位置是在按钮内部,我不想要这个,我想获取它的值值并将其保存到数据库并在按下按钮之前检索它,您能帮我吗?
public class currency extends Activity{
int item1,
item2;
private String array_spinner[];
private String array_spinner2[];
public Double rates[],
obj4;
public String API_URL2,
rates_new[],
s_query;
public SQLiteDatabase db;
@ Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.currency1);
array_spinner = new String[38];
rates = new Double[38];
rates_new = new String[38];
array_spinner[0] = " USD US Dollar";
rates[0] = 1.0;
db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null);
db.execSQL("DROP TABLE IF EXISTS FunnyNames");
db.execSQL("CREATE TABLE IF NOT EXISTS FunnyNames (id INTEGER, Email DOUBLE, FirstName VARCHAR, LastName VARCHAR);");
db.execSQL("INSERT INTO FunnyNames VALUES('0','" + rates[0] + "', 'Anita', 'Bath');");
for (int i = 1; i <= 37; i++) {
final int j = i;
API_URL2 = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USD"
+ array_spinner2[i]
+ "%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";
AsyncHttpClient client = new AsyncHttpClient();
client.get(API_URL2, new AsyncHttpResponseHandler() {
@ Override
public void onFailure(Throwable arg0, String arg1) {
super.onFailure(arg0, arg1);
}
@ Override
public void onFinish() {
super.onFinish();
}
@ Override
public void onStart() {
super.onStart();
}
@ Override
public void onSuccess(String response) {
try {
JSONObject jsonObj = new JSONObject(response);
JSONObject obj1 = jsonObj.getJSONObject("query");
JSONObject obj2 = obj1.getJSONObject("results");
JSONObject obj3 = obj2.getJSONObject("rate");
obj4 = obj3.getDouble("Rate");
rates[j] = obj4;
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
for (int k = 0; k <= 37; k++) {
s_query = "INSERT INTO FunnyNames VALUES('" + k + "','" + rates[k] + "', 'Anita', 'Bath');";
db.execSQL(s_query);
String select_query = "SELECT * from FunnyNames WHERE id=" + k;
Cursor c = db.rawQuery(select_query, null);
c.moveToFirst();
rates_new[k] = c.getString(1);
}
final Spinner s1 = (Spinner)findViewById(R.id.spinner23);
ArrayAdapter adapter1 = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, array_spinner);
s1.setAdapter(adapter1);
adapter1.setDropDownViewResource(R.layout.spinner_layout);
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView < ? > argo0, View arg1,
int arg2, long arg3) {
int item = s1.getSelectedItemPosition();
item1 = item;
}
public void onNothingSelected(AdapterView < ? > arg0) {}
});
final Spinner s2 = (Spinner)findViewById(R.id.spinner24);
ArrayAdapter adapter2 = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, array_spinner);
s2.setAdapter(adapter2);
adapter2.setDropDownViewResource(R.layout.spinner_layout);
s2.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView < ? > argo0, View arg1,
int arg2, long arg3) {
int item = s2.getSelectedItemPosition();
item2 = item;
}
public void onNothingSelected(AdapterView < ? > arg0) {}
});
Button currnecy_convert = (Button)findViewById(R.id.Button12);
currnecy_convert.setOnClickListener(new OnClickListener() {
@ Override
public void onClick(View arg0) {
final EditText input_currency = (EditText)findViewById(R.id.editText11);
final TextView converted_currency = (TextView)findViewById(R.id.textView61);
if (!input_currency.getText().toString().equals("")) {
converted_currency.setText(String.format(" " + rates_new[6] + " " + rates_new[20] + " " + rates_new[31]));
} else {
Toast.makeText(getApplicationContext(), "Please enter a USD value!", Toast.LENGTH_LONG).show();
}
}
});
}
}
最佳答案
您在数据库中得到null
,因为您对 Yahoo API 的调用是异步的。在从雅虎获得带有数组的答案之前,您正在插入数据库。您必须将填充数据库的 for 循环内容移至 AsyncHttpResponseHandler.onSuccess(String response)
方法中。
关于java - 无法访问数组的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22996978/