无法从片段转换为适配器(从onResponse转换片段)

时间:2017-09-23 14:24:06

标签: android android-fragments

将对象从Fragment通过onResponse转换为适配器时出现错误,我想使用{{1}在Fragment中创建搜索}

SearchView.OnQueryTextListener

此代码来自java.lang.ClassCastException: dca.application.taaku.Main cannot be cast to dca.application.taaku.temp.paging.utils.PaginationAdapterCallback at dca.application.taaku.hr.fragment.Approval.Sick.PaginationSearchAdapter.<init>(PaginationSearchAdapter.java:66) at dca.application.taaku.hr.fragment.Approval.Sick.SickFragmentToAll$6.onResponse(SickFragmentToAll.java:317)

SickFragmentToAll

这是我的适配器

     public boolean onQueryTextChange(String newText) {

        recyclerView.setVisibility(View.GONE);
        progressBar.setVisibility(View.VISIBLE);
        RegisterAPI api = apiRetrofit.getClient().create(RegisterAPI.class);

        LeaveGroup = param.LeaveGroup_Sick;
        Status = param.Approved;
        Flag = param.Active;

        Call<Root> call = api.searchApproval(LeaveGroup,Status, Flag, newText);
        call.enqueue(new Callback<Root>() {
            @Override
            public void onResponse(Call<Root> call, Response<Root> response) {
                int value = response.body().getValue();
                Log.d(TAG, "LOG onQueryTextChange : " + value);
                progressBar.setVisibility(View.GONE);
                recyclerView.setVisibility(View.VISIBLE);
                if (value == 1) {
                    Searchadapter = new PaginationSearchAdapter(getActivity());
                    linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
                    recyclerView.setLayoutManager(linearLayoutManager);
                    recyclerView.setItemAnimator(new DefaultItemAnimator());
                    recyclerView.setAdapter(adapter);
                    if (CURRENT_PAGE <= TOTAL_PAGES)
                        adapter.addLoadingFooter();
                    else
                        isLastPage = true;
                }else if (value == 2){
                    recyclerView.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void onFailure(Call<Root> call, Throwable t) {
                progressBar.setVisibility(View.GONE);
            }

        });
        return true;
    }

我已经尝试了

public PaginationSearchAdapter(Context context) {
    this.context = context;
    this.mCallback = (PaginationAdapterCallback) context; // The problem here
    results = new ArrayList<>();
}

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

但它无效

[UPDATE] 我之前在Android Can not be cast From Fragment to Adapter (Cast fragment into class)上问了这个问题,我设法找到答案(我用核对清单标记了),但它是public PaginationAdapter(Context context, Fragment f) { this.context = context; this.mCallback = (PaginationAdapterCallback) f; // The problem here results = new ArrayList<>(); } ,现在问题我需要{{1} }}

[编辑]

这是一个愚蠢的错误,我忘了导入onCreateView课程,它已成功解决,感谢帮助我的用户

1 个答案:

答案 0 :(得分:1)

确保对象为“mCallback”的类正在实现PaginationAdapterCallback。如果重复使用具有多个活动的片段,还要确保片段正确附加到活动。