gpt4 book ai didi

android - Recyclerview 布局在 fragment 中不起作用

转载 作者:行者123 更新时间:2023-11-30 00:17:09 25 4
gpt4 key购买 nike

我在 fragment 中遇到了关于 RecyclerView 的问题。我将 recyclerviewactivity 移到了 fragment。移动到 fragment 后。显示错误

No adapter attached; skipping layout

这是我的代码

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

最佳答案

No adapter attached; skipping layout

因为在将 Adapter 设置为您的 之后,您正在将数据添加到您的 Arraylist Recyclerview网络操作也需要时间响应

You need First add Data in Your propertyInfoList than set Adapter to Your RecyclerView

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

关于android - Recyclerview 布局在 fragment 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47054533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com