gpt4 book ai didi

android - RecyclerView android 上的 notifyDataSetChanged

转载 作者:行者123 更新时间:2023-11-30 01:24:32 32 4
gpt4 key购买 nike

我在 Recyclerview 上的 notifyDataSetChanged 中的代码有问题。这是我的代码,请帮助我修复它。

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)

最佳答案

在将适配器设置为 RecyclerView 之前对其进行初始化:

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

关于android - RecyclerView android 上的 notifyDataSetChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36590657/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com