Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

android recylerview not displaying firebase data

I am trying to retrieve data from another firebase project, but the data is not displaying in the recylerview. I am not getting any errors but just a blank cardview. Can someone help me solve this issue that I am having. My code is below.

// This my code

public class ExampleFragment extends Fragment {


    LinearLayout linearLayoutWithoutItems,linearLayoutNoConnection;
    View rootView;

    private RecyclerView recyclerView;
    private Adapter1 aAdapter;
    ImageButton menu_click;
    ImageView imageView;
    //Variables
    NavigationView navigationView;
    Toolbar toolbar;
    Menu menu;
    LinearLayout linearLayout;
    TextView textView;
    BottomNavigationView bottomNavigationView;
    private DatabaseReference databaseReference;
    private ArrayList<Model_Information> myUploads;
    FirebaseStorage firebaseStorage;
    StorageReference storageReference;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        rootView =inflater.inflate(R.layout.fragment_example, container, false);



        init();
        return rootView;
    }
    public void init()
    {

        recyclerView = rootView.findViewById(R.id.WithItems_recyclerview);
        linearLayoutWithoutItems = rootView.findViewById(R.id.WithoutItems);
        linearLayoutNoConnection = rootView.findViewById(R.id.no_connection);

        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getContext(),1);

        recyclerView.setLayoutManager(layoutManager);
        myUploads = new ArrayList<Model_Information>();
        aAdapter = new Adapter1(getContext(), myUploads);
        recyclerView.setAdapter(aAdapter);
        aAdapter.notifyDataSetChanged();

      
        databaseReference = FirebaseDatabase.getInstance("https://liou-43081.firebaseio.com").getReference().child("Post");

      


        if (InternetConnection.checkConnection(getContext())) {
            databaseReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {




                    if(dataSnapshot.exists()){
                        //progressBar.setVisibility(View.GONE);



                        for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {









                                Model_Information upload=postsnapshot.getValue(Model_Information.class);
                                //myUploads.clear();

                                myUploads.add(upload);
                                aAdapter = new Adapter1(getContext(), myUploads);
                                recyclerView.setAdapter(aAdapter);
                                aAdapter.notifyDataSetChanged();
                                recyclerView.invalidate();







                            }

                            linearLayoutWithoutItems.setVisibility(View.GONE);
                            recyclerView.setVisibility(View.VISIBLE);








                            aAdapter.notifyDataSetChanged();

                    }else{
                        linearLayoutWithoutItems.setVisibility(View.VISIBLE);
                        recyclerView.setVisibility(View.GONE);




                    }










                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    Toast.makeText(getContext(), databaseError.getMessage(), Toast.LENGTH_LONG).show();

                }
            });

        } else {

            linearLayoutNoConnection.setVisibility(View.VISIBLE);
            linearLayoutWithoutItems.setVisibility(View.GONE);
            recyclerView.setVisibility(View.GONE);


        }



  //Adapter1.class

  public class Adapter1 extends  RecyclerView.Adapter<Adapter1.ImageViewHolder>{

    private Context mContext;
    private ArrayList<Model_Information> users;
    DatabaseReference databaseReference;

    public Adapter1(Context context, ArrayList<Model_Information> uploads){
        mContext = context;
        users = uploads;

    }


    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View V = LayoutInflater.from(mContext).inflate(R.layout.cardview1, parent, false);
        return new ImageViewHolder(V);


    }

    @Override
    public void onBindViewHolder(@NonNull final ImageViewHolder holder, final int position) {
        //String uploadCurrent=users.get(position).getmImageUrl();

      

        Glide.with(mContext).load(users.get(position).getmImageUrl()).thumbnail(0.05f).transition(DrawableTransitionOptions.withCrossFade()).fitCenter().centerInside().into(holder.imageView);



        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(mContext,users.get(position).getCategory(), Toast.LENGTH_LONG).show();
                Long l= Long.valueOf(1);


                databaseReference = FirebaseDatabase.getInstance("https://louie-43081.firebaseio.com").getReference("Clicks_and_Views").child(users.get(position).id);
                databaseReference.child("views").setValue(ServerValue.increment(l));
/*

                Intent intent=new Intent(mContext,ViewActivity.class);
                intent.putExtra("website",users.get(position).getWebsiteurl());
                intent.putExtra("action",users.get(position).getAction());
                intent.putExtra("image",users.get(position).getmImageUrl());
                intent.putExtra("id",users.get(position).id);
                mContext.startActivity(intent);


 */








            }
        });






    }

    @Override
    public int getItemCount() {
        return users.size();

    }

    public class ImageViewHolder extends RecyclerView.ViewHolder{
        public ImageView imageView;
      

        public ImageViewHolder(@NonNull View itemView) {
            super(itemView);

            imageView=itemView.findViewById(R.id.image);
         


        }
    }
question from:https://stackoverflow.com/questions/65926049/android-recylerview-not-displaying-firebase-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should not initialize adapter twice. Your code block should look like this,

for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
     Model_Information upload=postsnapshot.getValue(Model_Information.class);
   

     myUploads.add(upload);
     aAdapter.notifyDataSetChanged();
     recyclerView.invalidate();

     }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...