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
Related
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'm performing some queries on the firebase database with the Firebase Adapter and Recyclerview. If I have the results in the database with edittext I'm viewing them with recyclerview. That's my problem. I want to display a toast on the screen if a query I'm calling with Edittext is not included in the database. I've written some codes, but I can't run it completely. The answers to other questions in Stackoverflow did not work for me and I could not understand the problem. Please review my code below. I'd appreciate it if you could help. Sorry for my bad english.
MainActivity
public class MainActivity extends AppCompatActivity {
DatabaseReference mUserDatabase;
EditText Search_Edit_Text;
ImageButton Search_Button;
RecyclerView Search_Contact_List;
FirebaseRecyclerOptions<User> options;
FirebaseRecyclerAdapter<User,CategoryViewHolder> adapter;
Query firebaseSearchQuery;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Database");
Search_Edit_Text = (EditText) findViewById(R.id.Search_Edit_Text);
Search_Button = (ImageButton) findViewById(R.id.Search_Button);
Search_Contact_List = (RecyclerView) findViewById(R.id.Search_Contact_List);
Search_Contact_List.setHasFixedSize(true);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getBaseContext(),1);
Search_Contact_List.setLayoutManager(gridLayoutManager);
Search_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String searchText = Search_Edit_Text.getText().toString().trim();
firebaseUserSearch(searchText);
}
});
}
private void firebaseUserSearch(String searchText) {
firebaseSearchQuery = mUserDatabase.orderByChild("phone").startAt("" + searchText).endAt("" + searchText + "\uf8ff");
options = new FirebaseRecyclerOptions.Builder<User>()
.setQuery(firebaseSearchQuery,User.class)
.build();
adapter = new FirebaseRecyclerAdapter<User, CategoryViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull final CategoryViewHolder holder, int position, #NonNull final User model) {
holder.categoryname.setText(model.getName());
}
#NonNull
#Override
public CategoryViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_layout,parent,false);
return new CategoryViewHolder(itemView);
}
};
setCategory();
}
private void setCategory() {
adapter.startListening();
Search_Contact_List.setAdapter(adapter);
adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
#Override
public void onChanged() {
super.onChanged();
checkEmpty();
}
#Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
checkEmpty();
}
#Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
super.onItemRangeRemoved(positionStart, itemCount);
checkEmpty();
}
void checkEmpty() {
if (adapter.getItemCount() == 0){
Toast.makeText(MainActivity.this, "No Result", Toast.LENGTH_SHORT).show(); //This code does not work.
} else {
Toast.makeText(MainActivity.this, "OKEY", Toast.LENGTH_SHORT).show(); //This code works.
}
}
});
}
}
I wrote that some of the code above works. But the code doesn't work when I don't get any results. Some Stackoverflow answers say that I need to get a value with adapter.getCount () or adapter.getItemCount (), but I tried it, but it didn't work.
CategoryViewHolder.class
public class CategoryViewHolder extends RecyclerView.ViewHolder {
public TextView categoryname;
public CategoryViewHolder(View itemView) {
super(itemView);
categoryname = (TextView)itemView.findViewById(R.id.Search_Result_Name);
}
}
User.class
public class User {
public String name;
public String phone;
public User() {
}
public User(String name, String phone) {
this.name = name;
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
Hi i'm stuck on this part where i have 2 collections first one is users and second one is friends. I want my recyclerview to display my friends on the list with the help of current user because on currentUser other peoples id are stored and i want it to display it on the recyclerview but when it displays it i want to get their information from users where they have their username and email stored.
I want only friends collection ids to display on recyclerview with the help of current user id. Then i want to get values from users collection like their name and email value.
Friends collection
users collection
ADAPTER
public class FriendsAdapter extends FirestoreRecyclerAdapter {
private OnItemClicklistener onItemClicklistener;
public FriendsAdapter(#NonNull FirestoreRecyclerOptions<AllUsers> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull FriendsHolder holder, int position, #NonNull AllUsers model) {
holder.textViewUsername.setText(String.valueOf(model.getName()));
holder.textViewEmail.setText(model.getEmail());
holder.setAvatar(model.getAvatar());
}
#NonNull
#Override
public FriendsHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.all_user_list_display, viewGroup, false);
return new FriendsAdapter.FriendsHolder(view);
}
class FriendsHolder extends RecyclerView.ViewHolder {
TextView textViewUsername;
TextView textViewEmail;
ImageView imageViewAvatar;
public FriendsHolder(#NonNull View itemView) {
super(itemView);
textViewUsername = itemView.findViewById(R.id.all_user_username);
textViewEmail = itemView.findViewById(R.id.all_user_userEmail);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION && onItemClicklistener != null) {
onItemClicklistener.onItemClick(getSnapshots().getSnapshot(position), position);
}
}
});
}
public void setAvatar(String avatar) {
imageViewAvatar = (ImageView) itemView.findViewById(R.id.all_user_profile_image);
Picasso.get().load(avatar).into(imageViewAvatar);
}
}
public interface OnItemClicklistener {
void onItemClick(DocumentSnapshot snapshot, int position);
}
public void setOnItemClickListener(FriendsAdapter.OnItemClicklistener listener) {
onItemClicklistener = listener;
}
FriendListFragment
public class FriendsListFragment extends Fragment {
private FirebaseFirestore db;
private CollectionReference usersCollection;
private CollectionReference friendsCollection;
private DocumentReference friends;
private DocumentReference users;
private FirebaseUser current_user;
private FriendsAdapter adapter;
public SearchView search_friends;
public FriendsListFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_friends, container, false);
current_user = FirebaseAuth.getInstance().getCurrentUser();
//FIREBASE
db = FirebaseFirestore.getInstance();
usersCollection = db.collection("users");
friendsCollection = db.collection("friends");
friends = friendsCollection.document(current_user.getUid());
Query friendQuery = usersCollection;
FirestoreRecyclerOptions<AllUsers> options = new FirestoreRecyclerOptions.Builder<AllUsers>()
.setQuery(friendQuery, AllUsers.class)
.build();
adapter = new FriendsAdapter(options);
RecyclerView recyclerView = view.findViewById(R.id.friendsList);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
return view;
}
#Override
public void onStart() {
super.onStart();
adapter.startListening();
}
AllUsersClass
public class AllUsers {
public String name;
public String avatar;
public String email;
public String userId;
public AllUsers() {
}
public AllUsers(String name, String email, String avatar, String userId) {
this.name = name;
this.email = email;
this.avatar = avatar;
this.userId = userId;
}
public String getName() {
return name;
}
public String getAvatar() {
return avatar;
}
public String getEmail() {
return email;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
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'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.