我试图从这个被覆盖的方法中设置一个类变量。我知道这是在另一个线程中执行的。如果我从那里输出它,我会得到价值。如果我在下面的方法中输出它,则值为空。
如何以正确的方式设置此类变量?
public class MainActivity extends AppCompatActivity {
private String[] mRestTitles = new String[2];
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject rootObject = response.getJSONObject("_embedded");
JSONArray users = rootObject.getJSONArray("users");
for (int i = 0; i < users.length(); i++) {
JSONObject o = users.getJSONObject(i);
mRestTitles[i] = o.getString("firstName");
Log.d("item", mRestTitles[i]);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("erro", error.toString());
}
}
);
queue.add(request);
public ArrayList<Entry> generateEntryList(){
String[] entryTitles = getResources().getStringArray(R.array.entry_names);
String[] entryDescriptions = getResources().getStringArray(R.array.entry_description);
ArrayList<Entry> entries = new ArrayList<>();
for(int i = 0; i < mRestTitles.length; i++){
// mRestTitles is here null
Log.d("foo", mRestTitles[i]);
entries.add(new Entry(mRestTitles[i], entryDescriptions[i]));
}
return entries;
}