-1

Activity

public class Home extends AppCompatActivity {
RecyclerView recyclerView;
FloatingActionButton floatingActionButton1,;
BottomNavigationView bottomNavigationView;
List<Post> list;
MyAdapter adapter;
private DatabaseReference databaseReference;
ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    list=new ArrayList<>();
    recyclerView=findViewById(R.id.recycler_View);
    bottomNavigationView=findViewById(R.id.bottom_navigation);
    floatingActionButton1=findViewById(R.id.floatingActionButton);
    progressDialog=new ProgressDialog(this);
    progressDialog.setMessage("Loading.....");
    bottomNavigationView.setOnNavigationItemReselectedListener(new 
BottomNavigationView.OnNavigationItemReselectedListener() {
        @Override
        public void onNavigationItemReselected(@NonNull MenuItem menuItem) {
            switch(menuItem.getItemId())
            {
                case R.id.action_home:
                    Toast.makeText(Home.this, "home", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.action_profile:
                    Toast.makeText(Home.this, "profile", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.action_recent:
                    Toast.makeText(Home.this, "recent", Toast.LENGTH_SHORT).show();
                    break;
            }
        }

    });



    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    adapter = new MyAdapter(Home.this,list);
    recyclerView.setAdapter(adapter);


    //COde for Firebase post Retrieve

    databaseReference= FirebaseDatabase.getInstance().getReference("post");
    progressDialog.show();
    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            list.clear();
                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                    Post post = postSnapshot.getValue(Post.class);
                    list.add(post);

                }
                adapter.notifyDataSetChanged();
                progressDialog.dismiss();

        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

public void gotoupload(View view) {

    startActivity(new Intent(getApplicationContext(),UploadPost.class));
}  }

This is my adapter MyAdapter

package com.example.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.PostViewHolder> {

Context context;
List<Post> postlist;

public MyAdapter(Context context, List<Post> postlist) {
    this.context = context;
    this.postlist = postlist;
}

@Override
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view= LayoutInflater.from(context).inflate
            (R.layout.row_item,parent,false);
    PostViewHolder postViewHolder=new PostViewHolder(view);
    return postViewHolder;
}

@Override
public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
        Post post=postlist.get(position);
        holder.textViewTitle.setText(post.getTextTitle());
        holder.textViewName.setText( post.getTextAuthor());
        holder.textViewdesc.setText(post.getTextDesc());
}

@Override
public int getItemCount() {
    return 0;
}

class PostViewHolder extends RecyclerView.ViewHolder{
    ImageView imageViewprofile, imageViewpost;
    TextView textViewName,textViewTitle,textViewdesc;

    public PostViewHolder(@NonNull View itemView) {
        super(itemView);
        imageViewpost=itemView.findViewById(R.id.Postimage);
        imageViewprofile=itemView.findViewById(R.id.profile_Rec);
        textViewdesc=itemView.findViewById(R.id.description);
        textViewName=itemView.findViewById(R.id.username1);
        textViewTitle=itemView.findViewById(R.id.Rtitle);

    }
}}

This is My model class Model class Named Post package com.example.myapplication;

public class Post {

public String postimageView;
public String proimage;
public String textTitle;
public String textAuthor;
public String textDesc;
public String textUrl;

public Post(){

}
public Post(String postimageView, String proimage, String textTitle, String textAuthor, String 
textDesc, String textUrl) {
    this.postimageView = postimageView;
    this.proimage = proimage;
    this.textTitle = textTitle;
    this.textAuthor = textAuthor;
    this.textDesc = textDesc;
    this.textUrl = textUrl;
}

public String getPostimageView() {
    return postimageView;
}

public void setPostimageView(String postimageView) {
    this.postimageView = postimageView;
}

public String getProimage() {
    return proimage;
}

public void setProimage(String proimage) {
    this.proimage = proimage;
}

public String getTextTitle() {
    return textTitle;
}

public void setTextTitle(String textTitle) {
    this.textTitle = textTitle;
}

public String getTextAuthor() {
    return textAuthor;
}

public void setTextAuthor(String textAuthor) {
    this.textAuthor = textAuthor;
}

public String getTextDesc() {
    return textDesc;
}

public void setTextDesc(String textDesc) {
    this.textDesc = textDesc;
}

public String getTextUrl() {
    return textUrl;
}

public void setTextUrl(String textUrl) {
    this.textUrl = textUrl;
}}

I am new to in android I want to read data from fire base but when I use recycler view my activity does not show any thing in activity.

I cant understand why my recyclerview is not show anything In the Activity. Help me pls.

theduck
  • 2,589
  • 13
  • 17
  • 23
Aakash Gupta
  • 1
  • 1
  • 5

2 Answers2

0

In My Adapter class in the method getItemCount() it says return 0;

Change that to the size of the list like

@Override
public int getItemCount() {
   return postlist.size();
}
Masoom Badi
  • 986
  • 9
  • 18
0

Here could be a problem (MyAdapter class):

@Override
public int getItemCount() {
    return 0;
}

getItemCount() should return a number of items in your adapter in order to render any. Try this:

@Override
public int getItemCount() {
    return this.postlist.size();
}

Of course, this.postlist.size() should be > 0 in order to render any of the items

pavle
  • 909
  • 14
  • 38