安卓分页 : Using APIs with Retrofit and Gson (Crash)

标签 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 fragment 类文件如下:

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()); // Issue raises here

    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)类 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;// Issue raises here

    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());

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

我无法弄清楚为什么会发生崩溃。可以做些什么来解决这个问题?

提前致谢。

最佳答案

adapter = new PaginationAdapter(getContext());
TopRated 调用的

getContext() Fragment 实际上返回一个 MovieTuneActivity 的对象,正如您所说,它没有实现接口(interface) 分页适配器回调。这就是您得到此异常的原因。

因为 TopRated Fragment 实现了这个接口(interface),我想你想做的是:

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

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

    adapter = new PaginationAdapter(getContext(), this);
    

这应该可以完成工作。尽管我不确定在 Adapter 中存储对 Fragment 的引用是个好主意。

关于安卓分页 : Using APIs with Retrofit and Gson (Crash),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45842577/

相关文章:

android - 使用 RxJava 2 和 Retrofit 2,适配器版本问题

java - Android 中的 SAX 解析问题...空元素?

php - jquery bootgrid 中的搜索结果为空

java - 更改 Spring JPA 的分页方式

reactjs - 如何在 Redux 中构建分页状态

android - Retrofit2:使用拦截器在查询参数中添加访问 token

java - 如何每 2 秒更改一次 ProgressDialog 的消息?

java - MPAndroidChart:如何创建分组条形图?

Android 应用因漏洞问题拒绝 google play

android - 使用接口(interface)而不是类定义 Retrofit 返回类型