使用RecyclerView改进Android JSON解析

时间:2016-12-30 20:55:08

标签: android json android-recyclerview retrofit2

我最近学到了一些关于使用retrofit2进行json解析的知识。我尝试自己实现,但没有显示输出,也没有错误在日志猫。可能有什么不对?看看下面的代码:

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private RestClient restClient;
private List<Movies> moviesList = new ArrayList<>();
private MoviesAdapter adapter;

private final String TAG = MainActivity.class.getCanonicalName();
private RelativeLayout relativeLayout;

private static final String API_KEY = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    relativeLayout = (RelativeLayout) findViewById(R.id.activity_main);

    if (API_KEY.isEmpty()){
        Snackbar.make(relativeLayout, "Access Requires an API Key", Snackbar.LENGTH_LONG);
    }

    recyclerView = (RecyclerView) findViewById(R.id.rView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    adapter = new MoviesAdapter(MainActivity.this, moviesList, R.layout.movie_items_layout);
    recyclerView.setAdapter(adapter);

    restClient = new RestClient();
    RestEndpoint restService = restClient.getMoviesClient().create(RestEndpoint.class);
    Call<MoviesResponse> moviesResponseCall = restService.getTopRatedMovies(API_KEY);
    moviesResponseCall.enqueue(new Callback<MoviesResponse>() {
        @Override
        public void onResponse(Call<MoviesResponse> call, Response<MoviesResponse> response) {

            moviesList.clear();
            moviesList.addAll(response.body().getMoviesResult());
            List<Movies> list = response.body().getMoviesResult();
            for (Movies movies : list){
                Movies movies1 = new Movies(movies.isAdult(), movies.getOverview(), movies.getReleaseDate(),
                        movies.getTitle(), movies.getBackdropPath(), movies.getVoteAverage());
            }
            adapter.notifyDataSetChanged();

            //moviesList = response.body().getMoviesResult();

            Log.v(TAG, "Number of Movies Received:" + moviesList.size());

        }

        @Override
        public void onFailure(Call<MoviesResponse> call, Throwable t) {

        }
    });

}

}

电影数据模型类如下所示:

public class Movies {

@SerializedName("poster_path")
private String posterPath;

@SerializedName("adult")
private boolean adult;

@SerializedName("overview")
private String overview;

@SerializedName("release_date")
private String releaseDate;

@SerializedName("genre_ids")
private List<Integer> genreIds;

@SerializedName("id")
private int id;

@SerializedName("original_title")
private String originalTitle;

@SerializedName("original_language")
private String originalLanguage;

@SerializedName("title")
private String title;

@SerializedName("backdrop_path")
private String backdropPath;

@SerializedName("popularity")
private double popularity;

@SerializedName("vote_count")
private String voteCount;

@SerializedName("video")
private boolean video;

@SerializedName("vote_average")
private double voteAverage;

public Movies(boolean adult, String overview, String releaseDate, String title, String backdropPath, double voteAverage) {
    this.adult = adult;
    this.overview = overview;
    this.releaseDate = releaseDate;
    this.title = title;
    this.backdropPath = backdropPath;
    this.voteAverage = voteAverage;
}

public Movies() {
}

public boolean isAdult() {
    return adult;
}

public void setAdult(boolean adult) {
    this.adult = adult;
}

public String getOverview() {
    return overview;
}

public void setOverview(String overview) {
    this.overview = overview;
}

public String getReleaseDate() {
    return releaseDate;
}

public void setReleaseDate(String releaseDate) {
    this.releaseDate = releaseDate;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getBackdropPath() {
    return backdropPath;
}

public void setBackdropPath(String backdropPath) {
    this.backdropPath = backdropPath;
}

public double getVoteAverage() {
    return voteAverage;
}

public void setVoteAverage(double voteAverage) {
    this.voteAverage = voteAverage;
}

}

另请查看我的recyclerview适配器类:

public class MoviesAdapter extends RecyclerView.Adapter<MoviesHolder> {

private Context context;
private List<Movies> moviesList;
private int rowLayout;

public MoviesAdapter(Context context, List<Movies> moviesList, int rowLayout) {
    this.context = context;
    this.moviesList = moviesList;
    this.rowLayout = rowLayout;
}

@Override
public MoviesHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View mView = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
    return new MoviesHolder(mView);
}

@Override
public void onBindViewHolder(MoviesHolder holder, int position) {
    Movies movies = moviesList.get(position);
    holder.title.setText(movies.getTitle());
    holder.date.setText(movies.getReleaseDate());
    holder.desription.setText(movies.getOverview());
    holder.rating.setText(String.valueOf(movies.getVoteAverage()));

    Picasso.with(context)
            .load(movies.getBackdropPath())
            .into(holder.backdrop);
}

@Override
public int getItemCount() {
    moviesList = new ArrayList<>();
    return moviesList.size();
}

}

使用api密钥从himoviedb网站获取json数据。请指出什么是错的。

1 个答案:

答案 0 :(得分:1)

你错了

@Override
public int getItemCount() {
    moviesList = new ArrayList<>();
    return moviesList.size();
}

您正在创建新列表,其大小为0,因此您的适配器始终认为没有元素

尝试删除moviesList = new ArrayList<>();