簡體   English   中英

無法訪問數組的值

[英]Can not access the value of an array

我正在嘗試編寫貨幣轉換器,我想從雅虎財務那里獲取信息,然后將其保存到數據庫中。 到目前為止,一切正常工作,除了一件事,就是我無法訪問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();
                }
            }
        });
    }
}

由於您對Yahoo API的調用是異步的,因此在數據庫中您將獲得null 您要插入數據庫,然后才能從Yahoo獲得有關陣列的答案。 您必須將for循環的內容移動到填充數據庫的AsyncHttpResponseHandler.onSuccess(String response)方法中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM