RecyclerView不能绑定适配器啊,写了适配器,但是一直报我没有适配器

时间:2021-01-19 20:04:44
package com.luck.yangming.fragment;

import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.luck.yangming.R;
import com.luck.yangming.adapter.MyOrderAdapter;
import com.luck.yangming.api.ApiManager;
import com.luck.yangming.api.HostType;
import com.luck.yangming.model.BookingOrderItem;
import com.luck.yangming.model.ResultData;
import com.luck.yangming.model.SingleResultData;
import com.luck.yangming.model.response.CommonResponse;
import com.luck.yangming.util.Tools;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/2/24.
 */
public class BookOrderFragment extends BaseFragment {
    @BindView(R.id.rl_order)
    RecyclerView rlOrder;
    @BindView(R.id.mSwipRL_Mian_Content)
    SwipeRefreshLayout mSwipRLMianContent;
    private Boolean isFirstCreat = true;
    private View view;
    private boolean isRefresh = false;
    private MyOrderAdapter myOrderAdapter;
    private String type;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this, super.onCreateView(inflater, container, savedInstanceState));
            getData();
        init();
            initRefreshListView();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_book_order;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if(isFirstCreat) {
            isFirstCreat=false;
            Bundle bundle = getArguments();
            if (bundle != null) {
                type = bundle.getString("type");
                Log.e("fragment取得值", type);
            }

        }
    }

    private void init() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        rlOrder.setLayoutManager(linearLayoutManager);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rlOrder.setAdapter(myOrderAdapter);
    }

    private void initRefreshListView() {
        mSwipRLMianContent.setColorSchemeColors(Color.BLUE);
        mSwipRLMianContent.setProgressViewOffset(true, 0, 100);
        mSwipRLMianContent.setRefreshing(true);
        mSwipRLMianContent.postDelayed(new Runnable() {

            @Override
            public void run() {
                //设置主动刷新
//                requestNetwork(1, typ);
                isRefresh = true;
                mSwipRLMianContent.setRefreshing(false);
                Log.e("-----------", "我执行了1");
            }
        }, 2000);
        mSwipRLMianContent.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mSwipRLMianContent.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getData();
                        stopRefresh();
//                        Log.e("----下拉刷新第-----",page+""+"页");
//                        myAdapter.notifyDataSetChanged();
                    }
                }, 3000);
            }
        });
    }
    private void stopRefresh() {
        if (!isRefresh) {
            return;
        }
        mSwipRLMianContent.setRefreshing(false);
    }

    /**
     * 请求数据
     */
    private void getData(){

        getData(type);
    }


    private void getData(final String type) {


        String time = com.luck.yangming.util.TimeUtils.getTimeToSmallS();
        Map<String, String> map = new HashMap<>();

        map.put("timestamp", time);
        map.put("accessToken", prefs.getAccessToken());
        map.put("source", "3");
        map.put("type", type);
        map.put("pageIndex", "1");
        map.put("pageSize", "6");
        String sign = Tools.getSign(map);
        map.put("sign", sign);

        Call<CommonResponse<ResultData<BookingOrderItem>>> call = ApiManager.getService(HostType.HTTP_NEW_BASE).getBookingList(map);
        call.enqueue(new Callback<CommonResponse<ResultData<BookingOrderItem>>>() {
            @Override
            public void onResponse(Call<CommonResponse<ResultData<BookingOrderItem>>> call, Response<CommonResponse<ResultData<BookingOrderItem>>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().isSuccess()) {
                        List<BookingOrderItem> result = response.body().data.result;

                        myOrderAdapter = new MyOrderAdapter(result,getActivity());
                        myOrderAdapter.notifyDataSetChanged();
                    } else {
                        String msg = response.body().message;
                        if (!TextUtils.isEmpty(msg)){
                            Log.e("ProductSystem", msg );
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<CommonResponse<ResultData<BookingOrderItem>>> call, Throwable t) {

            }
        });
    }
}

2 个解决方案

#1


写哪儿了? 没找到 RecyclerView不能绑定适配器啊,写了适配器,但是一直报我没有适配器

#2


myOrderAdapter = new MyOrderAdapter(result,getActivity());构造方法放的位置不对,你放在了异步回调中,主线程里setadapter的时候,你还没回调成功呢,要写在setadapter之前,先给个空list,回调里只去Notify就可以了

#1


写哪儿了? 没找到 RecyclerView不能绑定适配器啊,写了适配器,但是一直报我没有适配器

#2


myOrderAdapter = new MyOrderAdapter(result,getActivity());构造方法放的位置不对,你放在了异步回调中,主线程里setadapter的时候,你还没回调成功呢,要写在setadapter之前,先给个空list,回调里只去Notify就可以了