Firebase数据库无法在模拟器/设备中检索

时间:2018-03-09 16:21:15

标签: java android firebase firebase-realtime-database

Firebase database screenshot link

我无法检索设备中的数据这是壁纸应用程序 我也更新了谷歌播放服务,但没有任何反应。 我也给了互联网许可。 我还在模拟器和物理设备上进行了测试。 Firebase数据库截图链接位于顶部。

CategoryFragment.java

public class CategoryFragment extends Fragment {

FirebaseDatabase database;
DatabaseReference categoryBackground;

//Firebase UI adpter
FirebaseRecyclerOptions<CategoryItem> options;
FirebaseRecyclerAdapter<CategoryItem, CategoryViewHolder> adapter;

//View
RecyclerView recyclerView;


private static CategoryFragment INSTANCE=null;

public CategoryFragment() {
    database = FirebaseDatabase.getInstance();
    categoryBackground = 
database.getReference(Common.STR_CATEGORY_BACKGROUND);

    options = new FirebaseRecyclerOptions.Builder<CategoryItem>()
            .setQuery(categoryBackground, CategoryItem.class)
            .build();

    adapter = new FirebaseRecyclerAdapter<CategoryItem, CategoryViewHolder>
(options) {
        @Override
        protected void onBindViewHolder(@NonNull final CategoryViewHolder 
holder, int position, @NonNull final CategoryItem model) {
            Picasso.with(getActivity())
                    .load(model.getImageLink())
                    .networkPolicy(NetworkPolicy.OFFLINE)
                    .into(holder.background_image, new Callback() {
                        @Override
                        public void onSuccess() {

                        }

                        @Override
                        public void onError() {
                            //Try again if cache failed to load
                            Picasso.with(getActivity())
                                    .load(model.getImageLink())
                                    .error(R.drawable.ic_terrain_black_24dp)
                                    .into(holder.background_image, new 
Callback() {
                                        @Override
                                        public void onSuccess() {

                                        }

                                        @Override
                                        public void onError() {
                                            Log.e("ERROR_MSU", "Couldn't 
fetch image");

                                        }
                                    });

                        }
                    });

            holder.category_name.setText(model.getName());

            holder.setItemClickListener(new ItemClickListener() {
                @Override
                public void onClick(View view, int position) {
                    //Code later for detail category
                }
            });


        }

        @Override
        public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int 
 viewType) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.layout_category_item,parent,false);
            return new CategoryViewHolder(itemView);
        }
    };
}

public static CategoryFragment getInstance()
{

    if(INSTANCE == null)
    INSTANCE = new CategoryFragment();
    return INSTANCE;
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_category, container, 
false);
    recyclerView = (RecyclerView)view.findViewById(R.id.recycler_category);
    recyclerView.setHasFixedSize(true);
    GridLayoutManager gridLayoutManager = new 
GridLayoutManager(getActivity(),2);
    recyclerView.setLayoutManager(gridLayoutManager);

    setCategory();

    return view;
}

private void setCategory() {
    adapter.startListening();
    recyclerView.setAdapter(adapter);
}

@Override
public void onStart() {
    super.onStart();
    if (adapter!=null)
        adapter.startListening();
}

@Override
public void onStop() {
    if (adapter!=null)
        adapter.stopListening();
    super.onStop();

}

@Override
public void onResume() {
    super.onResume();
    if (adapter!=null)
        adapter.stopListening();
  }
}

CategoryItem.java

public class CategoryItem {

public String name;
public String imageLink;

public CategoryItem() {
}

public CategoryItem(String name, String imageLink){
    this.name = name;
    this.imageLink = imageLink;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getImageLink() {
    return imageLink;
}

public void setImageLink(String imageLink) {
    this.imageLink = imageLink;
 }
}

CategoryViewHolder

public class CategoryViewHolder  extends RecyclerView.ViewHolder implements 
View.OnClickListener {
public TextView category_name;
public ImageView background_image;

ItemClickListener itemClickListener;



public void setItemClickListener(ItemClickListener itemClickListener) {
    this.itemClickListener = itemClickListener;
}

public CategoryViewHolder(View itemView) {
    super(itemView);
    background_image = (ImageView)itemView.findViewById(R.id.image);
    category_name = (TextView)itemView.findViewById(R.id.name);

    itemView.setOnClickListener(this);
}

@Override
public void onClick(View v){
    itemClickListener.onClick(v,getAdapterPosition());

 }
}

0 个答案:

没有答案