视图未在gridlayout recyclerview中填充

时间:2018-06-20 01:04:34

标签: android android-recyclerview android-gridview

我正在尝试从api在线加载数据,但是视图加载存在问题。当我尝试运行模拟器时,出现空白屏幕。知道为什么吗?我猜我的问题在我如何加载gridview的某个地方,但是我无法弄清楚。

MainActivity

public class MainActivity extends AppCompatActivity {

    String SEARCH_TERM = "popular";
    private RecyclerView mRecyclerView;
    private MovieAdapter mMovieAdapter;
    private GridLayoutManager gridLayoutManager;
    private List<Movie> movieData;

    /*
    API KEY
    https://api.themoviedb.org/3/movie/550?api_key=1f5029b7d824dee72f4d4a156dac90ed
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecyclerView = findViewById(R.id.recycler_view);

        gridLayoutManager = new GridLayoutManager(this, 2);
        mRecyclerView.setLayoutManager(gridLayoutManager);

        mMovieAdapter = new MovieAdapter(this, movieData);

        mRecyclerView.setAdapter(mMovieAdapter);

        movieData = new ArrayList<>();

        loadMovieData();
    }

    private void loadMovieData() {
        new FetchMovieTask().execute(SEARCH_TERM);
    }

    public class FetchMovieTask extends AsyncTask<String, Void, Void> {
        @Override
        protected Void doInBackground(String... strings) {
            final String MOVIES_RESULTS = "results";
            final String MOVIES_POSTER_IMAGE = "poster_path";
            final String MOVIES_TITLE = "title";
            final String RELEASE_DATE = "release_date";
            URL moviesUrl = NetworkUtils.buildUrl(SEARCH_TERM);
            try {
                String jsonMoviesResponse = NetworkUtils.getReponseFromHttpUrl(moviesUrl);

                JSONObject moviesJson = new JSONObject(jsonMoviesResponse);

                JSONArray moviesArray = moviesJson.getJSONArray(MOVIES_RESULTS);

                for (int i = 0; i < moviesArray.length(); i++) {

                    String moviePoster;
                    String movieTitle;
                    String movieReleaseDate;
                    JSONObject movie = moviesArray.getJSONObject(i);

                    moviePoster = ("http://image.tmdb.org/t/p/w185/" + movie.getString(MOVIES_POSTER_IMAGE));
                    movieTitle = movie.getString(MOVIES_TITLE);
                    movieReleaseDate = movie.getString(RELEASE_DATE);

                    Log.i("MoviteTitle", movieTitle);
                    Log.i("ReleaseDate", movieReleaseDate);
                    Log.i("Image", moviePoster);

                    Movie data = new Movie(movieTitle, movieReleaseDate, moviePoster);

                    movieData.add(data);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            mMovieAdapter.notifyDataSetChanged();
        }
    }
}

适配器类

public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MovieAdapterViewHolder>{

    private List<Movie> mMovieData;
    private Context context;

    public MovieAdapter(Context context, List<Movie> movieData) {
        this.context = context;
        this.mMovieData = movieData;

    }

    @Override
    public void onBindViewHolder(@NonNull MovieAdapterViewHolder holder, int position) {
        holder.mTitleTextView.setText(mMovieData.get(position).getTitle());
        holder.mReleaseDateTextView.setText(mMovieData.get(position).getDate());
        Picasso.with(context).load(mMovieData.get(position).getMoviePoster()).into(holder.mImageView);

    }

    @NonNull
    @Override
    public MovieAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.cardview_item, parent, false);
        return new MovieAdapterViewHolder(view);
    }

    @Override
    public int getItemCount() {
        if (null == mMovieData)
            return 0;
        return mMovieData.size();
    }

    public class MovieAdapterViewHolder extends RecyclerView.ViewHolder {

        public final TextView mTitleTextView;
        public final TextView mReleaseDateTextView;
        public final ImageView mImageView;

        public MovieAdapterViewHolder(View itemView) {
            super(itemView);

            mTitleTextView = itemView.findViewById(R.id.title_text_view);
            mReleaseDateTextView = itemView.findViewById(R.id.release_date_text_view);
            mImageView = itemView.findViewById(R.id.image_data);
        }
    }
}

电影课

public class Movie {
    private String Title;
    private String Date;
    private String MoviePoster;

    public Movie(String title, String date, String moviePoster) {
        Title = title;
        Date = date;
        MoviePoster = moviePoster;
    }

    public Movie() {
    }

    public String getTitle() {
        return Title;
    }

    public String getDate() {
        return Date;
    }

    public String getMoviePoster() {
        return MoviePoster;
    }

    public void setTitle(String title) {
        Title = title;
    }

    public void setDate(String date) {
        Date = date;
    }

    public void setMoviePoster(String moviePoster) {
        MoviePoster = moviePoster;
    }
}

1 个答案:

答案 0 :(得分:2)

信息:Java都是按值传递的。

问题:您正在将空列表引用传递给适配器,然后初始化列表,但是在适配器中它仍然为null,因此适配器将永远不会在列表中收到添加的值,

setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.recycler_view);

gridLayoutManager = new GridLayoutManager(this, 2);
mRecyclerView.setLayoutManager(gridLayoutManager);

movieData = new ArrayList<>();
//^^^^^^^^^^^^^^^^^^^^^^^^^ do it before
mMovieAdapter = new MovieAdapter(this, movieData);
mRecyclerView.setAdapter(mMovieAdapter);    
loadMovieData();