Recyclerview布局不在片段中工作

时间:2017-11-01 12:11:44

标签: android android-fragments android-recyclerview recycler-adapter recyclerview-layout

我正面临片段中RecyclerView的问题。 我将recyclerviewactivity移到了fragment。 移动到片段后。 它显示错误

  

没有连接适配器;跳过布局

这是我的代码

public class LatestProperty extends Fragment {
    View customView;
    private List<PropertyInfo> propertyInfoList=new ArrayList<>();
    private RecyclerView recyclerView;
    private PropertyAdapter propertyAdapter;
    View view;
    ProgressDialog progressDialog;
    String areaType;
    private String imagePath="abc";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.total_properties, container, false);

        recyclerView = (RecyclerView)view.findViewById(R.id.recycler_view);
        progressDialog=new ProgressDialog(getActivity(),R.style.MyAlertDialogStyle);

        propertyAdapter=new PropertyAdapter(getActivity(),propertyInfoList);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(propertyAdapter);
        preparePropertyData();


        return view;
    }

public void preparePropertyData()
    {
        progressDialog.setMessage("Loading Latest Properties...");
        progressDialog.setCancelable(false);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.show();
        Config config=new Config();
        Volley.newRequestQueue(getActivity()).add(new StringRequest(config.getLatestPropertyData
                , new Response.Listener<String>() {
            public void onResponse(String response) {
                progressDialog.dismiss();
                try {
                    JSONArray jsonArray=new JSONArray(response);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        String area=jsonArray.getJSONObject(i).getString("area");
                        area=area.replace("null","NA");
                        String Gallery=    jsonArray.getJSONObject(i).getString("gallery");
                        String[] dot=Gallery.split(";");
                        String stringPrice=jsonArray.getJSONObject(i).getString("price");
                        stringPrice=stringPrice.replace(",","").replace("null","0");
                        Long price=Long.parseLong(stringPrice);
                        String areaType=jsonArray.getJSONObject(i).getString("area_type").replace("null","");
                        PropertyInfo propertyInfo=new PropertyInfo(jsonArray.getJSONObject(i).getString("title"),jsonArray.getJSONObject(i).getString("bed"),jsonArray.getJSONObject(i).getString("bath"),area,areaType,jsonArray.getJSONObject(i).getString("address"),jsonArray.getJSONObject(i).getString("purpose"),withSuffix(price).toString(), imagePath+dot[0],jsonArray.getJSONObject(i).getString("id"));
                        propertyInfoList.add(propertyInfo);

                    }
                    propertyAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getActivity(), e.getMessage(),Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            public void onErrorResponse(VolleyError error) {
                progressDialog.dismiss();
                CookieBar.Build(getActivity()).setTitle("Oppss. :(").setMessage("Server Error..!").setBackgroundColor(R.color.cookieColor).show();
            }
        }));


    }

这是适配器类

public PropertyAdapter(Context context,List<PropertyInfo> propertyInfoList) {
        this.mContext=context;
        this.propertyInfoList = propertyInfoList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recent_property_single_item, parent, false);


        return new MyViewHolder(itemView);
    }

2 个答案:

答案 0 :(得分:1)

  

没有连接适配器;跳过布局

因为您将 Arraylist 设置为 Adapter 后添加数据 Recyclerview 并且网络操作也需要时间进行响应

  

您需要首先在 propertyInfoList 中添加数据,而不是将 Adapter 添加到 RecyclerView

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    preparePropertyData();
    propertyAdapter=new PropertyAdapter(getActivity(),propertyInfoList);
    recyclerView.setAdapter(propertyAdapter);

答案 1 :(得分:0)

RecyclerView项发生异常时,请查看您的MyAdapter课程以及您的相关ViewHolder,并检查您是否正确地获得了TextView。< / p>

相关问题