Android分页:使用带有Retrofit和Gson的API(崩溃)

时间:2017-08-23 14:29:23

标签: android pagination retrofit2

我正在使用带有Retrofit和Gson的TMDB Movie API实现Android分页。一个名为"评分最高的电影"将使用TMDB API分页显示评分最高的电影图像。

记录:

FATAL EXCEPTION: main
                                               Process: me.saidur.movietune, PID: 23778
                                               java.lang.ClassCastException: me.saidur.movietune.activity.MovieTuneActivity cannot be cast to me.saidur.movietune.utils.PaginationAdapterCallback
                                                   at me.saidur.movietune.fragments.PaginationAdapter.<init>(PaginationAdapter.java:53)
                                                   at me.saidur.movietune.fragments.TopRated.onCreateView(TopRated.java:117)

TopRated.java片段类文件如下:

public class TopRated extends Fragment implements PaginationAdapterCallback {

private static final String TAG = NewRelease.class.getSimpleName();
// Inset your themoviedb.org api key
private final static String API_KEY = "...";


List<Movie> results;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_top_rated, container, false);

    progressBar = (ProgressBar) v.findViewById(R.id.main_progress);
    errorLayout = (LinearLayout) v.findViewById(R.id.error_layout);
    btnRetry = (Button) v.findViewById(R.id.error_btn_retry);
    txtError = (TextView) v.findViewById(R.id.error_txt_cause);
  

adapter = new PaginationAdapter(getContext()); //问题在这里提出

    recyclerView = (RecyclerView) v.findViewById(R.id.movies_recycler_view);
    linearLayoutManager = new LinearLayoutManager(getActivity());
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(linearLayoutManager);

    recyclerView.setAdapter(adapter);

    recyclerView.addOnScrollListener(new PaginationScrollListener(linearLayoutManager) {
        @Override
        protected void loadMoreItems() {
            isLoading = true;
            currentPage += 1;

            loadNextPage();
        }

        @Override
        public int getTotalPageCount() {
            return TOTAL_PAGES;
        }

        @Override
        public boolean isLastPage() {
            return isLastPage;
        }

        @Override
        public boolean isLoading() {
            return isLoading;
        }
    });

    //init service and load data
    apiService = ApiClient.getClient().create(ApiInterface.class);

    loadFirstPage();

    btnRetry.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            loadFirstPage();
        }
    });

    return v;
}

@Override
public void retryPageLoad() {
    loadNextPage();
}


private void loadFirstPage() {
    Log.d(TAG, "loadFirstPage: ");

   .....
   ....
}

private void loadNextPage() {
    .....
    .....
}

}

PaginationAdapter.java文件如下:

public class PaginationAdapter扩展了RecyclerView.Adapter {

// View Types
private static final int ITEM = 0;
private static final int LOADING = 1;
private static final int HERO = 2;

private static final String BASE_URL_IMG = "https://image.tmdb.org/t/p/w150";

private List<Movie> movieResults;
private Context context;

private boolean isLoadingAdded = false;
private boolean retryPageLoad = false;

private PaginationAdapterCallback mCallback;

private String errorMsg;

public PaginationAdapter(Context context) {
    this.context = context;
  

this.mCallback =(PaginationAdapterCallback)context; //问题引发   这里

    movieResults = new ArrayList<>();
}

public List<Movie> getMovies() {
    return movieResults;
}

public void setMovies(List<Movie> movieResults) {
    this.movieResults = movieResults;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder viewHolder = null;
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());

.................... .................... ....................(更多代码)

我无法弄清楚崩溃发生的原因。可以采取哪些措施来解决这个问题?

提前致谢。

1 个答案:

答案 0 :(得分:3)

adapter = new PaginationAdapter(getContext());
getContext()调用的{p> TopRated片段实际上返回MovieTuneActivity的对象,如您所述,该对象未实现接口PaginationAdapterCallback。这就是你得到这个例外的原因。

由于TopRated Fragment实现了这个接口,我认为你要做的是:

  1. PaginationAdapter类中,向构造函数添加第二个参数:

    public PaginationAdapter(Context context, PaginationAdapterCallback callback) {
        this.context = context;
        this.mCallback = callback;
        movieResults = new ArrayList<>();
    }
    
  2. TopRated片段中,将它的实例传递给适配器,因为片段实现了PaginationAdapterCallback接口:

    adapter = new PaginationAdapter(getContext(), this);
    
  3. 那应该做的工作。虽然我不确定在适配器中存储Fragment的引用是个好主意。