[英]Why i'm getting empty arrayList when called from another method android
我正在嘗試使用此方法GetList()填充ArrayList,我可以看到我添加了2條記錄。 但是,當我嘗試使用LoadQst()方法獲取這些記錄時,在該ArrayList中得到了0條記錄,這是我的代碼,
public class GameActivity extends AppCompatActivity {
TextView texto1,texto2;
String Option1,Option2;
int CurrentQst,CurrentQstId,int1,int2;
private RequestQueue mQueue;
Question qst = new Question();
public final List<Question> Questions = new ArrayList<Question>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
texto1 = (TextView) findViewById(R.id.red);
texto2 = (TextView) findViewById(R.id.blue);
CurrentQst=0;
mQueue = Volley.newRequestQueue(GameActivity.this);
texto1.setText("");
GetList();
LoadQst();
texto1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GetList();
}
});
}
private void GetList() {
String url = "*********/api/get_all_products.php";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("products");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject employee = jsonArray.getJSONObject(i);
qst = new Question();
String option1 = employee.getString("option1");
qst.id=employee.getInt("id");
qst.option1=employee.getString("option1");
qst.option2=employee.getString("option2");
qst.vote1=employee.getInt("vote1");
qst.vote2=employee.getInt("vote2");
boolean added = Questions.add(qst);
if (added)
{
texto1.append("added\n");
} else
{
texto1.append("not added\n");
}
}
for (int i = 0; i < Questions.size(); i++) {
texto1.append("option " + Questions.get(i).option1 + "\n");
}
texto1.append(" size "+Questions.size()); //here i get size of 2
//Collections.shuffle(Questions);
} catch (JSONException e) {
e.printStackTrace();
texto1.setText(e.getMessage());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mQueue.add(request);
}
private void LoadQst()
{
try {
Question CurrentQuestion ;
texto2.setText(String.valueOf(Questions.size()));//here i got an error size 0
// CurrentQuestion = (Question)Questions.get(CurrentQst);
//texto1.setText(CurrentQuestion.option1);
//texto2.setText(CurrentQuestion.option1);
// int1=CurrentQuestion.vote1;
//int2=CurrentQuestion.vote2;
//CurrentQstId=CurrentQuestion.id;
}catch (Exception e)
{
texto2.append(e.getMessage());
}
}
}
當我檢查我的問題中是否有東西時,我有2個結果
for (int i = 0; i < Questions.size(); i++) {
texto1.append("option " + Questions.get(i).option1 + "\n");
}
但是當我在此方法LoadQst()中調用此Questions ArrayList時,得到0個結果。 我應該從ArrayList切換到另一個方法嗎? 您將建議任何類型的解決方案
拜托,你可以看看嗎?
這是因為異步中的JsonObjectRequest
onResponse
方法。 LoadQst
方法在onResponse
方法之前執行。
打電話給你的LoadQst
內部方法onResponse
而不是調用中onCreate
。
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("products");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject employee = jsonArray.getJSONObject(i);
qst = new Question();
String option1 = employee.getString("option1");
qst.id=employee.getInt("id");
qst.option1=employee.getString("option1");
qst.option2=employee.getString("option2");
qst.vote1=employee.getInt("vote1");
qst.vote2=employee.getInt("vote2");
boolean added = Questions.add(qst);
if (added)
{
texto1.append("added\n");
} else
{
texto1.append("not added\n");
}
}
for (int i = 0; i < Questions.size(); i++) {
texto1.append("option " + Questions.get(i).option1 + "\n");
}
texto1.append(" size "+Questions.size()); //here i get size of 2
//Collections.shuffle(Questions);
LoadQst(); // Call here.
} catch (JSONException e) {
e.printStackTrace();
texto1.setText(e.getMessage());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.