简体   繁体   English

为什么从另一个方法android调用时我得到空的arrayList

[英]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. 我正在尝试使用此方法GetList()填充ArrayList,我可以看到我添加了2条记录。 but when I tried to get those records with this method LoadQst() I got 0 records in that ArrayList this is my code, 但是,当我尝试使用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());
        }

    }

}

when I check if I got something in my Questions I got 2 results 当我检查我的问题中是否有东西时,我有2个结果

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. 但是当我在此方法LoadQst()中调用此Questions ArrayList时,得到0个结果。 should I switch from ArrayList to another method? 我应该从ArrayList切换到另一个方法吗? you're to suggest any type of solutions 您将建议任何类型的解决方案

Please, can you take a look? 拜托,你可以看看吗?

This is because of JsonObjectRequest onResponse method in Asynchronous. 这是因为异步中的JsonObjectRequest onResponse方法。 The LoadQst method executes before the onResponse method. LoadQst方法在onResponse方法之前执行。

Call your LoadQst method inside onResponse instead of calling in onCreate . 打电话给你的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.

相关问题 从另一个 class 调用时,Static ArrayList 为空 - Static ArrayList being empty when called from another class 如果我从另一个类调用它,为什么我的ArrayList为空? - Why Am I getting my ArrayList empty if I call it from another class? 调用方法时,java ArrayList始终为空 - java ArrayList is always empty when method called 将字符串的ArrayList从一个活动传递到另一个活动时,为什么会出现空指针异常? - Why am I getting a null pointer exception when passing an ArrayList of strings from one activity to another? asynctask从处理程序调用时没有获得arraylist - asynctask not getting arraylist when called from a handler 从另一个类调用方法时出错 - I'm getting an error with calling a method from another class 为什么调用带有回调的方法时没有得到任何输出? - why am I not getting any output when a method with a callback is called? 为什么在控制器方法上调用getParameterTypes为空? - Why is getParameterTypes empty when called on a controller Method? 卡在从 android 中的另一个 class 获取 arraylist - stuck in getting arraylist from another class in android 为什么将arraylist称为optionPressed方法时将其清空? - Why the arraylist empties itself when it is called optionPressed method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM