无法访问数组的值

时间:2014-04-10 19:12:05

标签: java android

我正在尝试编写货币转换器,我想从yahoo finance获取信息,然后将其保存到数据库中。到目前为止一切都正常工作,除了一件事,我无法访问费率[k]的值,我唯一可以访问利率[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();
                }
            }
        });
    }
}

1 个答案:

答案 0 :(得分:3)

您在数据库中获得null,因为您对Yahoo API的调用是异步的。在您从阵列获得Yahoo的答案之前,您正在插入数据库。您必须将填充数据库的for循环内容移动到AsyncHttpResponseHandler.onSuccess(String response)方法。