【问题标题】:why this code shows an empty array list even though i added elements to it during request?为什么即使我在请求期间向其中添加了元素,此代码也会显示一个空数组列表?
【发布时间】:2020-01-26 06:21:36
【问题描述】:

我的代码是这样的,我不知道为什么它得到一个空数组,即使我在请求期间将元素添加到数组中。

公共类 MainActivity 扩展 AppCompatActivity {

 private List<Question> questionList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    questionList = new QuestionBank().getQuestions();
            Log.d("Main",  "processFinished: " + questionList);


}

// 请求 公共类 QuestionBank {

ArrayList<Question> questionArrayList = new ArrayList<>();
private String url = "https://raw.githubusercontent.com/curiousily/simple-quiz/master/script/statements-data.json";


public List<Question> getQuestions() {
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                    for (int i = 0; i < response.length(); i++) {
                        try {
                            Question question = new Question();
                            question.setAnswer(response.getJSONArray(i).get(0).toString());
                            question.setAnswerTrue(response.getJSONArray(i).getBoolean(1));


                            questionArrayList.add(question);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });

    AppController.getInstance().addToRequestQueue(jsonArrayRequest);
    return questionArrayList;
}
}

// 记录结果

D/Main: processFinished: []

【问题讨论】:

  • 您确定您的 JsonArrayRequest 将项目添加到数组中吗?尝试对其进行调试
  • 添加到列表时在 onResponse 中添加一条日志语句,您会看到问题
  • 添加回调接口并从onResponse(JSONArray response)中传递和调用

标签: java android arrays android-volley


【解决方案1】:

您可以通过以下方式传递回调

第 1 步:

创建一个网吧

public interface TestCallBack {
        void callBack(List<Question> response) ;
}

第 2 步: 创建匿名对象

TestCallBack testCallBack=new TestCallBack() {

            @Override
            public void callBack(List<Question> response) {
                // here you will get a response after success
            }
        };

并将此引用传递给

 questionList = new QuestionBank().getQuestions(testCallBack);
            Log.d("Main",  "processFinished: " + questionList);

第 3 步:

在服务器响应后调用此方法

public List<Question> getQuestions(TestCallBack testCallBack) { 

 public void onResponse(JSONArray response) {

 testCallBack.callBack(questionArrayList); // pass your array list here
 }

}

【讨论】:

    【解决方案2】:

    它是空的,因为您正在异步填充它。你应该添加一个回调作为参数,在onResponse中调用它。

    【讨论】:

    • 也许您可以扩展这个答案,了解如何完成?
    猜你喜欢
    • 2011-06-25
    • 1970-01-01
    • 2020-08-17
    • 2015-12-08
    • 2011-06-08
    • 2011-07-29
    • 1970-01-01
    • 2022-06-12
    • 2012-09-30
    相关资源
    最近更新 更多