我已将实时Firebase数据库与我的android应用程序链接。现在想在片段中的recyclerview中显示数据。我正在Log中获取数据,但是我不知道适配器有问题。
我一直在寻找有关我的问题的任何线索或想法。我的Logcat没有收到任何错误。这造成了更多的混乱。 另外,我是第一次使用实时Firebase来添加新技能。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
RecyclerView likes_recycler = (RecyclerView) view.findViewById(R.id.likes_recyclerview);
likesList = new ArrayList<>();
showLikesData();
likesAdapter = new BasedOnLikesAdapter(getContext(),likesList);
likes_recycler.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
likes_recycler.setAdapter(likesAdapter);
return view;
}
private void showLikesData() {
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
Query recentPostsQuery = databaseReference.child("business")
.limitToFirst(10);
recentPostsQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
Log.e("TEST", String.valueOf(dataSnapshot1));
basedOnLikes = dataSnapshot1.child("value").getValue(BasedOnLikes.class);
likesList.add(basedOnLikes);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getActivity(), "Something went wrong!", Toast.LENGTH_SHORT).show();
}
});
}
我的适配器看起来像这样
public class BasedOnLikesAdapter extends RecyclerView.Adapter<BasedOnLikesAdapter.LikesVH> {
private List<BasedOnLikes> basedOnLikesArrayList;
Context context;
public BasedOnLikesAdapter(Context context, List<BasedOnLikes> based) {
this.basedOnLikesArrayList = based;
this.context = context;
}
@NonNull
@Override
public BasedOnLikesAdapter.LikesVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.likes_home_cell, parent, false);
return new LikesVH(itemView);
}
@Override
public void onBindViewHolder(@NonNull BasedOnLikesAdapter.LikesVH holder, int position) {
BasedOnLikes item = basedOnLikesArrayList.get(position);
//Log.e("Item",(basedOnLikesArrayList.get(position)).toString());
holder.resName.setText(basedOnLikesArrayList.get(position).name);
}
@Override
public int getItemCount() {
return basedOnLikesArrayList.size();
}
public class LikesVH extends RecyclerView.ViewHolder {
private TextView resName;
//private TextView resCuisine;
private TextView resLocation;
private TextView resReviewCount;
private TextView city;
private TextView state;
private TextView is_open;
public LikesVH(View view) {
super(view);
//resImage = (ImageView) itemView.findViewById(R.id.res_img);
resName = (TextView) itemView.findViewById(R.id.res_title);
//resCuisine = (TextView) itemView.findViewById(R.id.res_cuisine);
resLocation = (TextView) itemView.findViewById(R.id.res_location);
resReviewCount = (TextView) itemView.findViewById(R.id.ratings_value);
city = (TextView) itemView.findViewById(R.id.res_city);
state = (TextView) itemView.findViewById(R.id.res_state);
}
}
我的吸气剂设置器文件
public class BasedOnLikes {
public String resImage;
public String name;
public String resCuisine;
public String address;
public int review_count;
public String city;
public String state;
public String is_open;
public long latitude;
public long longitude;
public BasedOnLikes(String resName, String resLocation,
int resReviewCount,String city, String is_open, long latitude, long longitude, String state) {
//this.resImage = resImage;
this.name = resName;
//this.resCuisine = resCuisine;
this.address = resLocation;
this.review_count = resReviewCount;
this.city = city;
this.is_open = is_open;
this.latitude = latitude;
this.longitude = longitude;
this.state = state;
}
public BasedOnLikes() {
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getIs_open() {
return is_open;
}
public void setIs_open(String is_open) {
this.is_open = is_open;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getResImage() {
return resImage;
}
public void setResImage(String resImage) {
this.resImage = resImage;
}
public int getReview_count() {
return review_count;
}
public void setReview_count(int review_count) {
this.review_count = review_count;
}
public long getLatitude() {
return latitude;
}
public void setLatitude(long latitude) {
this.latitude = latitude;
}
public long getLongitude() {
return longitude;
}
public void setLongitude(long longitude) {
this.longitude = longitude;
}
public String getResCuisine() {
return resCuisine;
}
public void setResCuisine(String resCuisine) {
this.resCuisine = resCuisine;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
只需要帮助就可以在我的回收站视图中显示数据。