简体   繁体   中英

Why i'm getting empty arrayList when called from another method android

I'm trying to fill an ArrayList with this method GetList() and I can see that I got 2 records added. but when I tried to get those records with this method LoadQst() I got 0 records in that ArrayList this is my code,

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());
        }

    }

}

when I check if I got something in my Questions I got 2 results

for (int i = 0; i < Questions.size(); i++) {
                                texto1.append("option " + Questions.get(i).option1 + "\n");
                        }

but when I call this Questions ArrayList in this method LoadQst() I get 0 results. should I switch from ArrayList to another method? you're to suggest any type of solutions

Please, can you take a look?

This is because of JsonObjectRequest onResponse method in Asynchronous. The LoadQst method executes before the onResponse method.

Call your LoadQst method inside onResponse instead of calling in 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();
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM