RecyclerView android上的notifyDataSetChanged

时间:2016-04-13 06:49:58

标签: android android-recyclerview notifydatasetchanged

notifyDataSetChanged Recyclerview上的代码存在问题。这是我的代码,请帮我解决。

public class FragmentListFilm extends BaseFragment {

    private static final String TAG = FragmentListFilm.class.getSimpleName();
    private static final String url "http://api.androidhive.info/json/movies.json";
    private List<InforFilm> inforFilms = new ArrayList<InforFilm>();
    private Fragment_Film_Adapter adapter;
    private DialogActivity dialogActivity;
    View root;
    private ProgressDialog pDialog;
    private RecyclerView rv;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_film, container, false);
        rv = (RecyclerView) root.findViewById(R.id._recycler_fragment_film);
        setUpRecycleView(rv);

        return root;
    }

    private void setUpRecycleView(RecyclerView recycleView) {
        recycleView.setLayoutManager(new LinearLayoutManager(recycleView.getContext()));
        recycleView.setAdapter(adapter);

        pDialog = new ProgressDialog(getActivity());
        // Showing progress dialog before making http request
        pDialog.setMessage("Loading...");
        pDialog.show();

       // recycleView.
        JsonArrayRequest movieReq = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        hidePDialog();

                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {
                               // Log.d(TAG, "try-cat");
                                JSONObject obj = response.getJSONObject(i);
                                InforFilm film = new InforFilm();

                                film.setTitle(obj.getString("title"));
                                film.setThumbail(obj.getString("image"));
                                film.setRating(((Number) obj.get("rating"))
                                        .doubleValue());
                                film.setYear(obj.getInt("releaseYear"));

                                // Genre is json array
                                JSONArray genreArry = obj.getJSONArray("genre");
                                ArrayList<String> genre = new ArrayList<String>();
                                for (int j = 0; j < genreArry.length(); j++) {
                                    genre.add((String) genreArry.get(j));
                                }
                                film.setGender(genre);

                                // adding movie to movies array
                                inforFilms.add(film);


                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        // notifying list adapter about data changes
                        // so that it renders the list view with updated data
                        Log.d(TAG, "notifydatasetchanged");
                        adapter.notifyDataSetChanged();

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                hidePDialog();

            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(movieReq);
    }

    private void hidePDialog() {
        if (pDialog != null) {
            pDialog.dismiss();
            pDialog = null;
        }
    }

}

//////适配器

public class Fragment_Film_Adapter extends RecyclerView.Adapter<Fragment_Film_Adapter.ViewHolder> {

    private List<InforFilm> inforFilms;
    ImageLoader imageLoader = AppController.getInstance().getmImageLoader();

    public Fragment_Film_Adapter(List<InforFilm> inforFilms) {
        this.inforFilms = inforFilms;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.film_thumbnail)
        NetworkImageView thumbNail;
        @Bind(R.id.film_genre)
        TextView gender;
        @Bind(R.id.film_rating)
        TextView rating;
        @Bind(R.id.film_title)
        TextView title;
        @Bind(R.id.film_releaseYear)
        TextView year;

        public ViewHolder(View view) {
            super(view);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_film_item, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        InforFilm infor = inforFilms.get(position);

        holder.thumbNail.setImageUrl(infor.getThumbail(), imageLoader);
        holder.title.setText(infor.getTitle());
        holder.rating.setText(String.valueOf(infor.getRating()));
        holder.year.setText(infor.getYear());
        String genreStr = "";
        for (String str : infor.getGender()) {
            genreStr += str + ", ";
        }
        genreStr = genreStr.length() > 0 ? genreStr.substring(0,
                genreStr.length() - 2) : genreStr;
        holder.gender.setText(genreStr);
    }

    @Override
    public int getItemCount() {
        return inforFilms.size();
    }


}

抛出以下异常:

Attempt to invoke virtual method 'void com.example.qtv.qtvmaterial.adapter.Fragment_Film_Adapter.notifyDataSetChanged()' on a null object reference
        at com.example.qtv.qtvmaterial.fragment.FragmentListFilm$1.onResponse(FragmentListFilm.java:110)
        at com.example.qtv.qtvmaterial.fragment.FragmentListFilm$1.onResponse(FragmentListFilm.java:71)

1 个答案:

答案 0 :(得分:1)

在将适配器设置为RecyclerView之前,先将其初始化:

recycleView.setLayoutManager(new LinearLayoutManager(recycleView.getContext()));
adapter = new Fragment_Film_Adapter(inforFilms);
recycleView.setAdapter(adapter);