我使用FirebaseUI填充RecyclerView
public class ViewRequestsFragment extends Fragment {
private Firebase myUserFire,myListFire;
protected RecyclerView.LayoutManager mLayoutManager;
private FirebaseRecyclerAdapter<Request,RecyclerViewHolder> recyclerAdapter;
public ViewRequestsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String firbaseUrl= getArguments().getString(Constants.FIREBASE);
myUserFire = new Firebase(firbaseUrl);
myListFire = new Firebase(Constants.FIREBASE_REQUESTS);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView= inflater.inflate(R.layout.fragment_view_requests, container, false);
final RecyclerView recyclerView= (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
FragmentActivity fragmentActivity=getActivity();
mLayoutManager = new LinearLayoutManager(fragmentActivity);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(mLayoutManager);
recyclerAdapter = new FirebaseRecyclerAdapter<Request, RecyclerViewHolder>(Request.class,R.layout.reques_list,RecyclerViewHolder.class, myListFire) {
@Override
protected void populateViewHolder(RecyclerViewHolder recyclerViewHolder, Request request, int i) {
recyclerViewHolder.tv1.setText(request.getRiderId());
recyclerViewHolder.tv2.setText(request.getDriverId());
}
};
recyclerView.setAdapter(recyclerAdapter);
return rootView;
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder {
TextView tv1, tv2;
public RecyclerViewHolder(View itemView) {
super(itemView);
tv1 = (TextView) itemView.findViewById(R.id.list_title);
tv2 = (TextView) itemView.findViewById(R.id.list_desc);
}
}
}
我收到以下错误
引起:com.fasterxml.jackson.databind.JsonMappingException:找不到类型[simple type,class com.google.android.gms.maps.model.LatLng]的合适构造函数:无法从JSON对象实例化(需要添加/启用类型信息?)
这是我的POJO课程
public class Request {
private String driverId;
private LatLng latLng;
public Request(){}
public Request(String driverId) {
this.driverId = driverId;
}
public Request(String driverId, LatLng latLng) {
this.driverId = driverId;
this.latLng = latLng;
}
public String getDriverId() {
return driverId;
}
public void setDriverId(String driverId) {
this.driverId = driverId;
}
public LatLng getLatLng() {
return latLng;
}
public void setLatLng(LatLng latLng) {
this.latLng = latLng;
}
}
这是我试图反序列化的JSON:
{
"driverId" : "null",
"latLng" : {
"latitude" : 38.421998333333335,
"longitude" : -121.08400000000002
}
}
我应该如何设计我的响应类以使用&#34; latLng&#34; ,任何帮助都会非常感激。
答案 0 :(得分:1)
您必须构建两个不同的模型:Request和LatLon。
请求:
public class Request implements Serializable {
private String driverId;
private LatLng latLng;
public Request(){}
public Request(String driverId) {
this.driverId = driverId;
}
public Request(String driverId, LatLng latLng) {
this.driverId = driverId;
this.latLng = latLng;
}
public String getDriverId() {
return driverId;
}
public void setDriverId(String driverId) {
this.driverId = driverId;
}
public LatLng getLatLng() {
return latLng;
}
public void setLatLng(LatLng latLng) {
this.latLng = latLng;
}
}
LatLng:
public class LatLng implements Serializable{
private String lat;
private String lon;
public String getLat() {
return lat;
}
public void setLat(String lat) {
this.lat = lat;
}
public String getLon() {
return lon;
}
public void setLon(String lon) {
this.lon = lon;
}
}
答案 1 :(得分:0)
public class LatLng {
public double latitude;
public double longitude;
public LatLng(){}
}
您可能还需要从Request
类
public class Request {
public String driverId;
public LatLng latLng;
public Request(){}
}
当您将类变量声明为public
时,您不必添加getter。您只需获取request.driverId