RecycleView Adapter在ArrayList中显示,带有JSON数据

时间:2017-10-08 20:06:01

标签: java android json android-adapter

我尝试为RecyclerView中显示的ArrayList创建RecyclerViewHolder 但我对代码底部的公共ArrayList有疑问。我应该在其中使用public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> { private static final String TAG = RecyclerViewAdapter.class.getSimpleName(); ArrayList<MovieItem> mMoviesItems; private Context context; public RecyclerViewAdapter(ArrayList<MovieItem> MoviesItems) { mMoviesItems = MoviesItems; } @Override public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { context = viewGroup.getContext(); int layoutIdForListItem = R.layout.movie_list_item; LayoutInflater inflater = LayoutInflater.from(context); boolean shouldAttachToParentImmediately = false; View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately); RecyclerViewHolder viewHolder = new RecyclerViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(RecyclerViewHolder holder, int position) { holder.MoviePopularity.setText(mMoviesItems.get(position).getPopularity(popularity)); Picasso.with(this.context).load(mMoviesItems.get(position).getPhoto(poster_path)).into(holder.MoviePoster); holder.MovieName.setText(mMoviesItems.get(position).getName(original_title)); } @Override public int getItemCount() { return mMoviesItems.size(); } class RecyclerViewHolder extends RecyclerView.ViewHolder { TextView MoviePopularity; ImageView MoviePoster; TextView MovieName; public RecyclerViewHolder(View itemView) { super(itemView); MoviePopularity = (TextView)itemView.findViewById(R.id.movie_popularity); MoviePoster = (ImageView)itemView.findViewById(R.id.iv_item_movie); MovieName = (TextView)itemView.findViewById(R.id.movie_name); } } } ,还是像下面的代码一样:

onBindViewHolder()

我也遇到问题public class MovieItem { private double popularity; private String photo; private String name; public MovieItem(double popularity, String poster_path, String original_title) { this.popularity = this.popularity; this.photo = photo; this.name = name; } public double getPopularity(double popularity) { return popularity; } public String getPhoto() { return photo; } public String getName() { return name; } } 我在poster_path,original_title和popular下遇到错误

这是我的自定义类代码:

Json

public final class OpenMovieJsonUtils { public static ArrayList<MovieItem> getSimpleMovieStringsFromJson(Context context, String moviesJsonString) throws JSONException { final String RESULTS = "results"; final String POPULARITY = "popularity"; final String POSTER_PATH = "poster_path"; final String ORIGINAL_TITLE = "original_title"; ArrayList<MovieItem> parsedMovieData = new ArrayList<MovieItem>(); JSONObject moviesObject = new JSONObject(moviesJsonString); JSONArray moviesArray = moviesObject.getJSONArray(RESULTS); for (int i = 0; i < moviesArray.length(); i++) { double popularity; String poster_path; String original_title; moviesObject = moviesArray.getJSONObject(i); popularity = moviesObject.getDouble(POPULARITY); poster_path = moviesObject.getString(POSTER_PATH); original_title = moviesObject.getString(ORIGINAL_TITLE); parsedMovieData.add(new MovieItem(popularity, poster_path, original_title)); } return parsedMovieData; } } 数据类:

default []

1 个答案:

答案 0 :(得分:0)

您正在将人气字段初始化为相同的全局字段,因此默认情况下始终为null。还要更改您的getter方法。更改您的MovieItem类,如下所示:

public class MovieItem {
private double popularity;
private String photo;
private String name;

public MovieItem(double popularity, String poster_path, String original_title) {
    this.popularity = popularity;
    this.photo = photo;
    this.name = name;
}

public double getPopularity() { return popularity; }
public String getPhoto() { return photo; }
public String getName() { return name; }

现在改变你的onBindViewHolder方法:

    @Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
    holder.MoviePopularity.setText(String.valueOf(mMoviesItems.g‌​et(position).getPopu‌​larity()));
    Picasso.with(this.context).load(mMoviesItems.get(position).getPhoto()).into(holder.MoviePoster);
    holder.MovieName.setText(mMoviesItems.get(position).getName());
}