I'm trying to show Blog Posts on my MainActivity (Home page). But Homepage Nothing shows I mean Homepage is empty.
Firebase database structure:
MainActivity:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private RecyclerView mBlogList;
private DatabaseReference mDataRef;
private FirebaseRecyclerAdapter<BlogInfo, ViewHolder> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: started!");
mDataRef = FirebaseDatabase.getInstance().getReference().child("BlogApp").child("Posts");
mBlogList = findViewById(R.id.blogListId);
mBlogList.setHasFixedSize(true);
mBlogList.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerOptions<BlogInfo> options = new FirebaseRecyclerOptions.Builder<BlogInfo>()
.setQuery(mDataRef, BlogInfo.class).build();
adapter = new FirebaseRecyclerAdapter<BlogInfo, ViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull ViewHolder holder, int position, #NonNull BlogInfo model) {
String imgUrl = model.getImage();
Picasso.get().load(imgUrl).into(holder.post_image);
holder.post_title.setText(model.getTitle());
holder.post_desc.setText(model.getDescription());
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Log.d(TAG, "onCreateViewHolder: initialized!");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_blog_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
};
mBlogList.setAdapter(adapter);
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
Log.d(TAG, "onStart: init");
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
Log.d(TAG, "onStop: stop Blog list");
}
// ViewHolder Class
public static class ViewHolder extends RecyclerView.ViewHolder{
private static final String TAG = "ViewHolder";
TextView post_title;
TextView post_desc;
ImageView post_image;
ViewHolder(View itemView) {
super(itemView);
Log.d(TAG, "ViewHolder: Initialized!");
post_title = itemView.findViewById(R.id.postTitleId);
post_desc = itemView.findViewById(R.id.postDescId);
post_image = itemView.findViewById(R.id.postImageId);
}
}
}
BlogInfo.class:
public class BlogInfo {
private String description;
private String image;
private String title;
public BlogInfo() {}
public BlogInfo(String title, String description, String image) {
this.title = title;
this.description = description;
this.image = image;
}
public String getTitle() {return title;}
public void setTitle(String title) {this.title = title;}
public String getDescription() {return description;}
public void setDescription(String description) {this.description = description;}
public String getImage() {return image;}
public void setImage(String image) {this.image = image;}
}
This is my first time I'm trying to use FirebaseRecyclerAdapter to retrieve data from Firebase Database. But I don't know where the problem is. I tried to debug also but the compiler won't enter into onCreateViewHolder or onBindViewHolder function. But I'm totally following FirebaseUI for Realtime Database reference.
To solve this, you need to remove the following line of code:
mBlogList.setHasFixedSize(true);
This is needed only when you want your RecyclerView to have a fixed size. In your case is not necessary.
Related
I'm a beginner in android development . i have created an app that display data(image and text ) from Firebase in RecyclerView . everything is working fine , there is also one data (url) and i want when the user will click on any item it will start new activity using Intent and will display this url in a Webview . i have created new activity and set up the WebView in it . i just don't know how to pass the url from firebase to this activity . i was searching for 3 days and i did know that i need to use setOnClickItemListener but i didn't find anything realted to my case . that's why i thought about posting it here .
sorry for my English . thank you
my Firebase data is like this
enter image description here
this is model.java
public class model {
String title, image, description ,url;
public model() {
this.url=url ;
this.title = title;
this.image = image;
this.description = description;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTitle() {
return title;
}
public String getImage() {
return image;
}
public String getDescription() {
return description;
}
public void setTitle(String title) {
this.title = title;
}
public void setImage(String image) {
this.image = image;
}
public void setDescription(String description) {
this.description = description;
}
}
and this is the RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>{
private Context mContext;
private List<model> modelList;
public RecyclerAdapter(Context context, List<model> listData) {
this.modelList = listData;
this.mContext = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.row,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.title.setText(modelList.get(position).getTitle());
holder.description.setText((modelList.get(position).getDescription()));
Picasso.get().load(modelList.get(position).getImage()).into(holder.imageView);
}
#Override
public int getItemCount() {
return modelList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title,description;
ImageView imageView;
public ViewHolder(View itemView){
super(itemView);
title=itemView.findViewById(R.id.Title);
description=itemView.findViewById(R.id.Description);
imageView=itemView.findViewById(R.id.ImageView);
}
}
}
and this is MainActivity.java
public class MainActivity extends AppCompatActivity {
private DatabaseReference reference ;
private StorageReference mStorageRef ;
private RecyclerView recyclerView;
private ArrayList<model> modelList;
private Context mContext = MainActivity.this;
private RecyclerAdapter recyclerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayoutManager layoutManager= new LinearLayoutManager(this );
recyclerView=findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
reference = FirebaseDatabase.getInstance().getReference();
mStorageRef = FirebaseStorage.getInstance().getReference();
modelList = new ArrayList<>();
init();
}
private void init(){
clearAll();
Query query = reference.child("Data");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
model rmodel = new model();
rmodel.setImage(snapshot.child("image").getValue().toString());
rmodel.setTitle(snapshot.child("title").getValue().toString());
rmodel.setUrl(snapshot.child("url").getValue().toString());
rmodel.setDescription(snapshot.child("description").getValue().toString());
modelList.add(rmodel);
}
recyclerAdapter = new RecyclerAdapter(mContext , modelList);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
private void clearAll(){
if (modelList !=null){
modelList.clear();
if(recyclerAdapter != null)
recyclerAdapter.notifyDataSetChanged();
}
modelList = new ArrayList<>();
}
}
In the onBindViewHolder method:
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.title.setText(modelList.get(position).getTitle());
holder.description.setText((modelList.get(position).getDescription()));
Picasso.get().load(modelList.get(position).getImage()).into(holder.imageView);
holder.addOnClickListener(new OnClickListener() {
#override
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(modelList.get(position).getUrl()));
startActivity(browserIntent);
}
});
}
This will add an action listener on the holder to open the URL in a browser.
Can't convert object of type java.lang.String to type sort.media.sound.audio.workshoplecture2.Contacts
This is the error I get.
import com.squareup.picasso.Picasso;
import de.hdodenhof.circleimageview.CircleImageView;
public class FindFriendActivity extends AppCompatActivity {
private Toolbar mToolBar;
private RecyclerView FindFriendsRecylerList;
private DatabaseReference UsersRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_friend);
UsersRef= FirebaseDatabase.getInstance().getReference().child("Users");
FindFriendsRecylerList=(RecyclerView) findViewById(R.id.find_friends_recycler_lists);
FindFriendsRecylerList.setLayoutManager(new LinearLayoutManager(this));
mToolBar=(Toolbar) findViewById(R.id.find_friends_toolbar);
setSupportActionBar(mToolBar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle("Find Friends");
}
#Override
public void onStart()
{
super.onStart();
FirebaseRecyclerOptions<Contacts> options=new FirebaseRecyclerOptions.Builder<Contacts>
().setQuery(UsersRef,Contacts.class).build();
FirebaseRecyclerAdapter<Contacts,FindFriendsViewHolder> adapter=new FirebaseRecyclerAdapter<Contacts, FindFriendsViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull FindFriendsViewHolder holder, int position, #NonNull Contacts model) {
model=new Contacts();
holder.userName.setText(model.getName());
holder.userStatus.setText(model.getStatus());
Picasso.get().load(model.getImage()).into(holder.profileImage);
}
#NonNull
#Override
public FindFriendsViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.users_display_layout,viewGroup,false);
FindFriendsViewHolder findFriendsViewHolder=new FindFriendsViewHolder(view);
return findFriendsViewHolder;
}
};
FindFriendsRecylerList.setAdapter(adapter);
adapter.startListening();
}
public static class FindFriendsViewHolder extends RecyclerView.ViewHolder
{
TextView userName,userStatus;
CircleImageView profileImage;
public FindFriendsViewHolder(#NonNull View itemView) {
super(itemView);
userName=itemView.findViewById(R.id.users_profile_name);
userStatus=itemView.findViewById(R.id.users_status);
profileImage=itemView.findViewById(R.id.users_profile_image);
}
}
}
My Contacts Class
package sort.media.sound.audio.workshoplecture2;
public class Contacts {
String name;
String image;
String status;
public Contacts()
{
}
public Contacts(String name, String image, String status) {
this.name = name;
this.image = image;
this.status = status;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
I want to show a list of group chats in a TabLayout fragment. So, in order to do that, I used FirebaseRecyclerAdapter as my adapter and put it on RecyclerView.
These are my dependencies:
implementation 'com.firebaseui:firebase-ui-database:4.1.0'
implementation 'com.google.firebase:firebase-database:16.0.1'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.android.support:support-v4:27.1.1
This is my firebase database structure
This is my model:
public class ChatConv {
private String image, last_msg, name, online_status;
public ChatConv() {
}
public ChatConv(String image, String last_msg, String name, String online_status) {
this.image = image;
this.last_msg = last_msg;
this.name = name;
this.online_status = online_status;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getLast_msg() {
return last_msg;
}
public void setLast_msg(String last_msg) {
this.last_msg = last_msg;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOnline_status() {
return online_status;
}
public void setOnline_status(String online_status) {
this.online_status = online_status;
}
}
I have created an adapter class that extends the FirebaseRecyclerAdapter and this is my code:
public class ChatConvAdapter extends FirebaseRecyclerAdapter<ChatConv, ChatConvAdapter.ViewHolder> {
private static final String TAG = "ChatConvAdapter";
public ChatConvAdapter(#NonNull FirebaseRecyclerOptions<ChatConv> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull ViewHolder holder, int position, #NonNull ChatConv model) {
holder.bind(model);
Log.i(TAG, "onBind is called");
Log.i(TAG, "name= " + model.getName());
Log.i(TAG, "msg= " + model.getLast_msg());
Log.i(TAG, "image= " + model.getImage());
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_item_global_list, parent, false);
Log.i(TAG, "onCreate is called");
return new ViewHolder(view);
}
class ViewHolder extends RecyclerView.ViewHolder {
#BindView(R.id.item_image)
CircleImageView imageView;
#BindView(R.id.item_name)
TextView nameView;
#BindView(R.id.item_desc)
TextView msgView;
#BindView(R.id.item_time)
TextView timeView;
ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
public void bind(ChatConv chatConv) {
String name = chatConv.getName();
String image = chatConv.getImage();
String msg = chatConv.getLast_msg();
nameView.setText(name);
msgView.setText(msg);
Picasso.get().load(image).noFade().into(imageView);
}
}
}
In my fragment, I initialized my adapter and set the adapter inside the onCreateView()
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.layout_chat_list, container, false);
unbinder = ButterKnife.bind(this, view);
initObjects();
setUpRecyclerView();
setUpAdapter();
return view;
}
private void setUpAdapter() {
Query query = mWorldChatRef.orderByKey();
FirebaseRecyclerOptions<ChatConv> options = new FirebaseRecyclerOptions.Builder<ChatConv>()
.setQuery(query, ChatConv.class).build();
mAdapter = new ChatConvAdapter(options);
listChatView.setAdapter(mAdapter);
}
And startListening/stopListening in onStart()/onStop();
#Override
public void onStart() {
super.onStart();
mAdapter.startListening();
}
#Override
public void onStop() {
super.onStop();
mAdapter.stopListening();
}
I've searched for the possible answer here in StackOverflow but it didn't work for my case. Why am not getting logs? Am I missing something?
I have found my own solution by adding this line to the class that extends Application.
#Override
public void onCreate() {
super.onCreate();
Timber.plant(new Timber.DebugTree());
FirebaseDatabase instance = FirebaseDatabase.getInstance();
instance.setPersistenceEnabled(true);
}
I was trying to load data from firebase into my recycler view. There are two values that I am retrieving first is Name and second is the ImageUrl. When i run my app the value from the name is displaying on the recyclerview but the image is still blank. Somehow picasso is failing to load the image into the CircleImageView.
Below is my ViewHolderclass
public static class CategoryViewHolder extends RecyclerView.ViewHolder {
View mView;
CircleImageView categoryImageView;
TextView categoryName;
public CategoryViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setDisplayName(String name){
categoryName = (TextView) mView.findViewById(R.id.Category_name);
categoryName.setText(name);
}
public void setUserImage(String thumb_image, Context ctx){
categoryImageView = (CircleImageView)
mView.findViewById(R.id.Category_pic);
Picasso.with(ctx).load(thumb_image).placeholder(R.drawable.
default_avatar)
.into( categoryImageView);
// Picasso.with(ctx) .load(thumb_image) .into(categoryImageView);
}
}
this is my model class
public class CategoryItems {
private String Name;
private String ImageUrl;
public CategoryItems(String title, String imageURL) {
this.Name = title;
this.ImageUrl = imageURL;
}
publ ic CategoryItems(){
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getImageUrl() {
return ImageUrl;
}
public void setImageUrl(String imageUrl) {
ImageUrl = imageUrl;
}
}
this is my FirebaseREcyclerViewAdapter
public void onStart() {
super.onStart();
startListening();
}
public void startListening(){
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("Catlist")
.limitToLast(500);
FirebaseRecyclerOptions<CategoryItems> options =
new FirebaseRecyclerOptions.Builder<CategoryItems>()
.setQuery(query, CategoryItems.class)
.build();
FirebaseRecyclerAdapter adapter = new
FirebaseRecyclerAdapter<CategoryItems, CategoryViewHolder>(options) {
#Override
public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.category_items, parent, false);
return new CategoryViewHolder(view);
}
#Override
protected void onBindViewHolder(final CategoryViewHolder holder, int
position, final CategoryItems model) {
holder.setDisplayName(model.getName());
holder.setUserImage(model.getImageUrl(), getContext());
// holder.setUserStatus(model.getStatus());
Log.d(model.getImageUrl().toString(), "onBindViewHolder: ");
}
};
mUsersList.setAdapter(adapter);
adapter.startListening();
}
my recycler is displaying names only and i tried to log the imageurls and its displaying the url fine
Try enabling logging in Picasso to look at errors and warning, it will definitely show some error in your case which you can resolve.
Picasso
.with(context)
.setLoggingEnabled(true)
....
For more information check out this link
Currently im learning android studio by making an application chat, but i'm stuck in one situation.
i want the app show all user that registered in firebase by using recyclerview, but here's the thing, the activity is working fine, but no user in the activity
build.gradle
implementation 'com.google.firebase:firebase-auth:11.8.0'
implementation 'com.google.firebase:firebase-database:11.8.0'
implementation 'com.google.firebase:firebase-storage:11.8.0'
implementation 'com.firebaseui:firebase-ui-database:3.2.2'
AllFriendActivity.java
public class AllFriendActivity extends AppCompatActivity {
private DatabaseReference mDatabaseReference;
private Toolbar mToolbar;
private RecyclerView mFriendList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_friend);
mToolbar = (Toolbar) findViewById(R.id.all_friend_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("All Friend");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("User");
mFriendList = (RecyclerView) findViewById(R.id.friend_list);
mFriendList.setHasFixedSize(true);
mFriendList.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<Friend> options = new FirebaseRecyclerOptions.Builder<Friend>()
.setQuery(mDatabaseReference, Friend.class)
.build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Friend, FriendViewHolder>(options) {
#NonNull
#Override
public FriendViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.friend_layout, parent, false);
Toast.makeText(AllFriendActivity.this, "Test Toast", Toast.LENGTH_SHORT).show();
return new FriendViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull FriendViewHolder holder, int position, #NonNull Friend model) {
holder.setName(model.getName());
holder.setStatus(model.getStatus());
}
};
mFriendList.setAdapter(adapter);
}
public static class FriendViewHolder extends RecyclerView.ViewHolder {
View mView;
public FriendViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setName(String name){
TextView friendDisplayName = (TextView) mView.findViewById(R.id.friend_display_name);
friendDisplayName.setText(name);
}
public void setStatus(String status){
TextView friendStatus = (TextView) mView.findViewById(R.id.friend_status);
friendStatus.setText(status);
}
}
}
Friend.java
public class Friend {
String name;
String status;
String avatar;
public Friend() {
}
public Friend(String name, String status, String avatar) {
this.name = name;
this.status = status;
this.avatar = avatar;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
}
i've search the solution in internet, but still cant fix the problem