Android Asynctask arraylist всегда возвращает 0 - PullRequest
0 голосов
/ 05 апреля 2020

У меня есть действие, где у меня есть URL, и с помощью библиотеки залпов я извлекаю данные JSON в ArrayList объектов класса модели DmIncomingTest. Теперь я вставил URL в метод checkLiveQuiz (), даже после получения данных с сервера и добавления их в ArrayList размер ArrayList всегда равен 0. Я делюсь кодом здесь:

private List<DmInComingTest> inComingTestList;

setContentView(R.layout.activity_no_live_quiz);
......

new MyAsyncTask().execute();

}

private class MyAsyncTask extends AsyncTask<Void, Void, Void> {
        private Context context;
        @Override
        protected void onPreExecute() {
            inComingTestList= new ArrayList<DmInComingTest>();
         //   dialog = ProgressDialog.show(NoLiveQuiz.this, "Please wait..", "Doing stuff..", true);
        }
        @Override
        protected Void doInBackground(Void... voids) {
        checkLiveQuiz();

        return null;
        }
        @Override
        protected void onPostExecute(Void result) {
           // Toast.makeText(NoLiveQuiz.this,"Size of inComingTestList :"+inComingTestList.size(),Toast.LENGTH_LONG).show();
            if(inComingTestList.size()==0)
            {
                 Toast.makeText(NoLiveQuiz.this,"Size of inComingTestList :"+inComingTestList.size(),Toast.LENGTH_LONG).show();
            }
            else
            {
                startActivity(new Intent(NoLiveQuiz.this,LiveQuiz.class));
            }
           // dialog.dismiss();

        }
    }
    public void checkLiveQuiz()
    {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Loading...");
        progressDialog.setCancelable(false);
        progressDialog.show();

        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url_live_quiz, new Response.Listener<JSONArray>() {

            @Override
            public void onResponse(JSONArray response) {
                for (int i = 0; i < response.length(); i++) {
                    try {
                        JSONObject jsonObject = response.getJSONObject(i);

                        DmInComingTest inComingTest = new DmInComingTest();
                        inComingTest.setTitle(jsonObject.getString("title"));
                        inComingTest.setTotal_questions(jsonObject.getString("qcount"));
                        inComingTest.setDated(jsonObject.getString("date_format(dated,'%d.%m.%Y')"));
                        inComingTest.setStartTime(jsonObject.getString("starttime"));
                        inComingTest.setEndTime(jsonObject.getString("endtime"));
                        inComingTest.setDuration(jsonObject.getInt("duration"));
                        inComingTest.setMaxMarks(jsonObject.getInt("maxmarks"));
                        if(validateTest(jsonObject.getString("date_format(dated,'%d.%m.%Y')"),jsonObject.getString("starttime"),jsonObject.getString("endtime"),jsonObject.getInt("duration")))
                        {
                            inComingTestList.add(inComingTest);
                            Toast.makeText(NoLiveQuiz.this,"Test Added:  "+ inComingTestList.size(),Toast.LENGTH_LONG).show();
                            // flag=1;
                            //       Toast.makeText(MainActivity.this,"Flag Inner="+flag,Toast.LENGTH_LONG).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        progressDialog.dismiss();
                    }
                }
                //      adapter.notifyDataSetChanged();
                progressDialog.dismiss();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("Volley", error.toString());
                //  progressDialog.dismiss();
            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(jsonArrayRequest);

    }
    private boolean validateTest(String testDate, String startTime, String endTime, int duration)
    {
        Date oldDate, newDate,newDate1;
        String oldTime,newTime,newTime1;
        long diff=0;
        long oldLong=0;
        long newLong=0;
        long newLong1=0;
        SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss");
        formatter.setLenient(false);
        Calendar c = Calendar.getInstance();
        oldTime=formatter.format(c.getTime());
        // Toast.makeText(MainActivity.this, ""+oldTime, Toast.LENGTH_SHORT).show();
        newTime=testDate +", "+startTime+":00";
        newTime1=testDate +", "+endTime+":00";
        //   Toast.makeText(MainActivity.this, ""+newTime, Toast.LENGTH_SHORT).show();
        try {
            oldDate = formatter.parse(oldTime);
            newDate = formatter.parse(newTime);
            newDate1=formatter.parse(newTime1);
            oldLong = oldDate.getTime();
            newLong = newDate.getTime();
            newLong1=newDate1.getTime();
            diff = newLong - oldLong;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //long timer= (long) Long.parseLong(String.valueOf(diff));
        //Toast.makeText(IncomingActivity.this, ""+timer, Toast.LENGTH_SHORT).show();
        if(oldLong>newLong && oldLong<newLong1)
        {
            return true;
        }
        else
            return false;
    }
...