即使在将对象添加到列表之后也要返回Null

时间:2019-02-06 14:38:46

标签: java android android-volley

我正在用Volley检索数据列表。问题在于,当我在Volley的onReponse方法之外登录时,列表为空。

我尝试使用吸气剂,但它们似乎没有做任何事情。

package com.disruption.globalmail;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
import com.disruption.globalmail.adapter.MyNewsAdapter;
import com.disruption.globalmail.model.News;
import com.disruption.globalmail.network.AppController;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


public class TopNewsFragment extends Fragment implements MyNewsAdapter.RecyclerViewClickListener {
    private static String TAG = TopNewsFragment.class.getSimpleName();

    private ProgressDialog mProgressDialog;

    /*Toast*/
    private Toast mToast;

    private List<News> mNewsList;

    public TopNewsFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_top_news, container, false);


        // Progress dialog
        mProgressDialog = new ProgressDialog(getContext());
        mProgressDialog.setMessage("Please wait...");
        mProgressDialog.setCancelable(false);

        fireApiRequest();

        // Inflate the layout for this fragment
        return rootView;
    }

    public void fireApiRequest() {
        showDialog();
        mNewsList = new ArrayList<>();

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
                MainActivity.createUrl(), null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray articles = response.getJSONArray("articles");

                    for (int i = 0; i < articles.length(); i++) {
                        JSONObject articleObject = articles.getJSONObject(i);

                        String newsTitle = articleObject.getString("title");
                        String newsDescription = articleObject.getString("description");

                        News news = new News(newsTitle, newsDescription);
                        mNewsList.add(news);
                    }

                    setUpRecView();

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                hideDialog();
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                hideDialog();
            }
        });
        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonObjReq);
    }


    private void setUpRecView() {
        RecyclerView recyclerView = Objects.requireNonNull(getActivity()).findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        final MyNewsAdapter newsAdapter = new MyNewsAdapter(getActivity(), mNewsList, this);
        recyclerView.setAdapter(newsAdapter);

        newsAdapter.notifyDataSetChanged();

    }

    private void showDialog() {
        if (!mProgressDialog.isShowing())
            mProgressDialog.show();
    }

    private void hideDialog() {
        if (mProgressDialog.isShowing())
            mProgressDialog.dismiss();
    }

    @Override
    public void onRecyclerViewItemClicked(View view, int position) {
        News feature = mNewsList.get(position);

        //Cancel a toast is the user clicks rapidly
        if (mToast != null) {
            mToast.cancel();
        }
        //Make a toast
        mToast = Toast.makeText(getActivity(), feature.getNewsTitle(), Toast.LENGTH_LONG);
        mToast.show();


    }
}

如何确保onRecyclerViewItemClicked方法中的mNewsList变量不为空?还是这只是Volley的问题?

更新 我决定取消Volley,然后去翻新

0 个答案:

没有答案