Android Studio 变量未从函数调用更新



这可能是一个基本的Java问题。 在相同的活动中,我声明了一个 String[] 数据,后来成功更新了它,但是当我尝试从更新数据的调用函数中将文本视图设置为 [1] 更新的数据 [1] 时 - 什么也没显示。 这是精简的代码。

    public class MyClass extends AppCompatActivity {
        String[] data = new String[4];
        public void populateGrid() {}
            getIndexData(indices);
            final TextView test = (TextView) findViewById(R.id.textView0B);
            test.post(new Runnable() {
                @Override
                public void run() {
                    test.setText(data[1]);
                }
            });
        public void getIndexData(final String[] indices){
             //lots of work accomplished, data[1] is updated, Log.d() logs good!
             // Tried passing data[] as a parameter from populateGrid(), but that didn't work.
             // Tried returning data[] to populateGrid(), also didn't work.
        }
    }

完成此任务的正确方法是什么?

根据要求,getIndexData((

    public void getIndexData(final String indices){
            mOkHttpClient = new OkHttpClient();
            HttpUrl reqUrl = HttpUrl.parse("http://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=" +
                    indices +
                    "&outputsize=compact&apikey=" +
                    apiKey);
            Request request = new Request.Builder().url(reqUrl).build();
            mOkHttpClient.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    // Show user error message if not connected to internet, et. al.
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Context context = getApplicationContext();
                            CharSequence text = getResources().getString(R.string.Toast_1);
                            int duration = Toast.LENGTH_LONG;
                            Toast toast = Toast.makeText(context, text, duration);
                            toast.show();
                        }
                    });
                }
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    int j = 0;
                    String responseBody = response.body().string();
                    if (responseBody.contains(""Error Message"")) {
                        data[j] = "No Data";
                        data[j+1] = "No Data";
                        data[j+2] = "No Data";
                        data[j+3] = "No Data";
                    } else { // Extract data points from json object.
                        try {
                            JSONObject baseObject = new JSONObject(responseBody);
                            JSONObject timeSeriesObj = baseObject.optJSONObject("Time Series (Daily)");
                            Iterator<String> iterator = timeSeriesObj.keys();
                            List<Map<String, String>> tickerData = new ArrayList<Map<String, String>>();
                            while (iterator.hasNext()) {
                                String key = iterator.next();
                                if (key != null) {
                                    HashMap<String, String> m = new HashMap<String, String>();
                                    JSONObject finalObj = timeSeriesObj.optJSONObject(key);
                                    m.put("1. open", finalObj.optString("1. open"));
                                    m.put("2. high", finalObj.optString("2. high"));
                                    m.put("3. low", finalObj.optString("3. low"));
                                    m.put("4. close", finalObj.optString("4. close"));
                                    m.put("5. volume", finalObj.optString("5. volume"));
                                    tickerData.add(m);
                                }
                            }
                            int k = 0;
                            String str = tickerData.get(0).toString();
                            data[k] = StringUtils.substringBetween(str, "open=", ", ");
                            //Log.d("data[0]= ", data[0]);
                            data[k+1] = StringUtils.substringBetween(str, "close=", ", ");
                            Log.d("data[1]", data[1]); // logs 2431.7700 
                            data[k+2] = ""; 
                            data[k+3] = "";

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
    }

它会是这样的:

public class MyClass extends AppCompatActivity {
    String[] data = new String[4];
    public void populateGrid() {
        getIndexData(indices);
    }
    public void getIndexData(final String indices) {
        // set up http request
        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                // ...
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                // process the response, populate data etc.
                final TextView test = (TextView) findViewById(R.id.textView0B);
                test.post(new Runnable() {
                    @Override
                    public void run() {
                        test.setText(data[1]);
                    }
                });
            }
        }
    }
}

最新更新