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);
}
Related
the firebase data doesn't appear in the recycler view
i have some realtimeDb in Google firebase
i try to read it in the recyclerView but nothing appears ,
logcat shows null and here is my code:
1-Here is HomeFragment(Main) Code
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
Unbinder unbinder;
#BindView(R.id.recycler_popular)
RecyclerView recyclerView;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
unbinder= ButterKnife.bind(this,root);
init();
homeViewModel.getPopularList().observe(this, new Observer<List<PopularCategoryModel>>() {
#Override
public void onChanged(List<PopularCategoryModel> PopularCategoryModel) {
//create adapter
MyPopularCategoriesAdapter adapter = new MyPopularCategoriesAdapter(HomeFragment.this.getContext(), PopularCategoryModel);
recyclerView.setAdapter(adapter);
}
});
return root;
}
private void init() {
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}
}
2-Here is HomeViewModel Code
public class HomeViewModel extends ViewModel implements IPopularCallbackListener {
private static final String TAG = "HomeViewModel";
private MutableLiveData<List<PopularCategoryModel>> popularList;
private MutableLiveData<String> messageError;
private IPopularCallbackListener popularCallbackListener;
public HomeViewModel() {
}
public MutableLiveData<List<PopularCategoryModel>> getPopularList() {
if (popularList== null){
popularList=new MutableLiveData<>();
messageError=new MutableLiveData<>();
loadPopularList();
}
return popularList;
}
private void loadPopularList() {
final List<PopularCategoryModel> tempList = new ArrayList<>();
DatabaseReference popularRef= FirebaseDatabase.getInstance().getReference(Common.POPULAR_CATEGORY_REF);
popularRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
tempList.clear();
for (DataSnapshot itemSnapShot:dataSnapshot.getChildren()){
PopularCategoryModel model=itemSnapShot.getValue(PopularCategoryModel.class);
tempList.add(model);
Log.d(TAG, "onDataChange:Adel2020"+tempList.size()+popularRef);
}
//popularCallbackListener.onPopularLoadSuccess(tempList);
Log.e(TAG, "Data received:" + tempList.size());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
popularCallbackListener.onPopularLoadFailed(databaseError.getMessage());
}
});
}
public MutableLiveData<String> getMessageError() {
return messageError;
}
#Override
public void onPopularLoadSuccess(List<PopularCategoryModel> popularCategoryModels) {
popularList.setValue(popularCategoryModels);
}
#Override
public void onPopularLoadFailed(String message) {
messageError.setValue(message);
}
}
3- This is the Adapter
public class MyPopularCategoriesAdapter extends RecyclerView.Adapter<MyPopularCategoriesAdapter.PopViewHolder> {
private List<PopularCategoryModel> listPopularCategoryModel ;
Context context;
public MyPopularCategoriesAdapter( Context context,List<PopularCategoryModel> listPopularCategoryModel) {
this.listPopularCategoryModel = listPopularCategoryModel;
this.context = context;
}
#NonNull
#Override
public PopViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new PopViewHolder(LayoutInflater.from(parent.getContext()).inflate
(R.layout.layout_popular_categories_item, parent, false));
}
#Override
public void onBindViewHolder(#NonNull PopViewHolder holder, int position) {
Glide.with(context).load(listPopularCategoryModel.get(position).getImage()).into(holder.popCategoryImage);
holder.popCategoryName.setText(listPopularCategoryModel.get(position).getName());
}
#Override
public int getItemCount() {
return listPopularCategoryModel.size();
}
public class PopViewHolder extends RecyclerView.ViewHolder {
Unbinder unbinder;
#BindView(R.id.pop_category_name)
TextView popCategoryName;
#BindView(R.id.pop_category_image)
CircleImageView popCategoryImage;
public PopViewHolder(#NonNull View itemView) {
super(itemView);
unbinder= ButterKnife.bind(this,itemView);
}
}
}
4-This is Model
public class PopularCategoryModel {
private String menu_id,food_id,name,image;
public PopularCategoryModel() {
}
public PopularCategoryModel(String menu_id, String food_id, String name, String image) {
this.menu_id = menu_id;
this.food_id = food_id;
this.name = name;
this.image = image;
}
public PopularCategoryModel(String name, String image) {
this.name = name;
this.image = image;
}
+SetterGetter
5-Here is the Data
I'm developing an android app for online order. User needs to click on an food item in recyclerView and I'm trying to send the food ID to the AddOrderActivity. How can I get the FId and pass it to the new activity?
CustomerHomeFragment.java
public class CustomerHomeFragment extends Fragment {
private RecyclerView recyclerView;
private FoodsAdapter adapter;
private List<Food> foodList;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home_customer, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
Call<FoodsResponse> call = RetrofitClient.getInstance().getApi().getFoods();
call.enqueue(new Callback<FoodsResponse>() {
#Override
public void onResponse(Call<FoodsResponse> call, Response<FoodsResponse> response) {
foodList = response.body().getFoods();
adapter = new FoodsAdapter(getActivity(), foodList);
recyclerView.setAdapter(adapter);
}
#Override
public void onFailure(Call<FoodsResponse> call, Throwable t) {
}
});
}
}
public class FoodsAdapter extends RecyclerView.Adapter implements View.OnClickListener{
private Context mCtx;
private List<Food> foodList;
public FoodsAdapter(Context mCtx, List<Food> foodList) {
this.mCtx = mCtx;
this.foodList = foodList;
}
#NonNull
#Override
public FoodsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mCtx).inflate(R.layout.recyclerview_foods, parent, false);
return new FoodsViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull FoodsViewHolder holder, int position) {
Food food = foodList.get(position);
holder.textViewFName.setText(food.getFName());
holder.textViewUnitPrice.setText(String.format("%d", food.getUnitPrice()));
holder.textViewAvailCount.setText(String.format("%d", food.getAvailCount()));
}
#Override
public int getItemCount() {
return foodList.size();
}
//////////////
#Override
public void onClick(View view) {
Intent intent = new Intent(this.mCtx, AddOrderActivity.class);
this.mCtx.startActivity(intent);
}
/////////////////
class FoodsViewHolder extends RecyclerView.ViewHolder {
TextView textViewFName, textViewUnitPrice, textViewAvailCount;
public FoodsViewHolder(View itemView) {
super(itemView);
textViewFName = itemView.findViewById(R.id.textViewFName);
textViewUnitPrice = itemView.findViewById(R.id.textViewUnitPrice);
textViewAvailCount = itemView.findViewById(R.id.textViewAvailCount);
}
}
}
public class Food {
private int fid, unitprice, availcount;
private String fname;
public Food(int fid, String fname, int unitprice, int availcount) {
this.fid = fid;
this.fname = fname;
this.unitprice = unitprice;
this.availcount = availcount;
}
public int getFId() {
return fid;
}
public String getFName() {
return fname;
}
public int getUnitPrice() {
return unitprice;
}
public int getAvailCount() {
return availcount;
}
}
in the your adapter class inside the onBindViewHolder function add this code
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(context,NoteActivity.class);
intent.putExtra("note_id", currentNote.getNoteId());
intent.putExtra("note_name",currentNote.getNoteName());
intent.putExtra("note_teacher",currentNote.getNoteTeacher());
intent.putExtra("note_university",currentNote.getNoteUniversity());
intent.putExtra("note_image",currentNote.getNoteImage());
intent.putExtra("note_is_downloaded", currentNote.getIsDownloaded());
intent.putExtra("note_storage_path", currentNote.getStoragePath());
intent.putExtra("note_download_link", currentNote.getNoteDownloadLink());
context.startActivity(intent);
}
});
I am developing an app that shows in a RecyclerView an image and a text that comes from firebase.
When I open the application the recyclerview does not appear but if I turn off and then turn on the screen the recyclerview appears correctly with the image and text obtained from firebase. I need help please
ViewHolder:
public class ViewHolderServicio extends RecyclerView.ViewHolder {
View mView;
public ViewHolderServicio(#NonNull View itemView) {
super(itemView);
mView = itemView;
}
public void setDetails(Context ctx, String nombreServicio, String imagenPerfil) {
TextView mNombreServicio = mView.findViewById(R.id.NombreServicio);
ImageView mImagenPerfil = mView.findViewById(R.id.imageViewServicio);
mNombreServicio.setText(nombreServicio);
Picasso.get().load(imagenPerfil).into(mImagenPerfil);
}
}
Model:
public class ModelServicio {
String idUsuario, nombreServicio, descripcion, imagenPerfil;
public ModelServicio(){}
public String getIdUsuario() {
return idUsuario;
}
public void setIdUsuario(String idUsuario) {
this.idUsuario = idUsuario;
}
public String getNombreServicio() {
return nombreServicio;
}
public void setNombreServicio(String nombreServicio) {
this.nombreServicio = nombreServicio;
}
public String getDescripcion() {
return descripcion;
}
public void setDescripcion(String descripcion) {
this.descripcion = descripcion;
}
public String getImagenPerfil() {
return imagenPerfil;
}
public void setImagenPerfil(String imagenPerfil) {
this.imagenPerfil = imagenPerfil;
}
}
class activity:
public class DetalleServicio extends AppCompatActivity {
RecyclerView RVbot;
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRef;
FirebaseRecyclerAdapter<ModelServicio, ViewHolderServicio> firebaseRecyclerAdapter;
FirebaseRecyclerOptions<ModelServicio> options;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detalle_servicio);
RVbot = findViewById(R.id.RVBot);
RVbot.setHasFixedSize(true);
RVbot.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRef = mFirebaseDatabase.getReference("Servicios");
mostrarDatos();
}
private void mostrarDatos() {
options = new FirebaseRecyclerOptions.Builder<ModelServicio>().setQuery(mRef, ModelServicio.class).build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<ModelServicio, ViewHolderServicio>(options) {
#Override
protected void onBindViewHolder(#NonNull ViewHolderServicio holder, int position, #NonNull ModelServicio model) {
holder.setDetails(getApplicationContext(), model.getNombreServicio(), model.getImagenPerfil());
}
#NonNull
#Override
public ViewHolderServicio onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.servicioitem, viewGroup, false);
ViewHolderServicio viewHolder = new ViewHolderServicio(itemView);
return viewHolder;
}
};
RVbot.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
}
#Override
protected void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
RVbot.setAdapter(firebaseRecyclerAdapter);
}
}
Have you tried
firebaseRecyclerAdapter.notifyDataSetChanged();
after calling an Adapter?
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
I am trying to fetch data from my Parse database and display it into a RecyclerView. From my knowledge I cannot see what I have done wrong and when I run the app not errors occur, and when opening this very fragment the app doesn't crash just shows a blank screen.
Any idea what is the problem?
Businesses
package zafir.com.app;
import com.parse.ParseClassName;
import com.parse.ParseObject;
#ParseClassName("Businesses")
public class Businesses extends ParseObject
{
private String Name;
public String getName()
{
return getString("Name");
}
public void setName(String name)
{
put("Name", name);
}
public String getCategory()
{
return getString("Category");
}
public void setCategory(String category)
{
put("Category", category);
}
public String getEmail()
{
return getString("Email");
}
public void setEmail(String email)
{
put("Email", email);
}
public String getLocation()
{
return getString("Location");
}
public void setLocation(String location)
{
put("Location", location);
}
public String getPhone()
{
return getString("Phone");
}
public void setPhone(String phone)
{
put("Phone", phone);
}
public String getWebsite()
{
return getString("Website");
}
public void setWebsite(String website)
{
put("Website", website);
}
}
RecyclerAdapter
public static class ViewHolder extends RecyclerView.ViewHolder
{
public TextView zName;
public TextView zPhone;
public TextView zEmail;
public TextView zWebsite;
public TextView zLocation;
public TextView zCategory;
public ViewHolder(View itemView)
{
super(itemView);
zName = (TextView) itemView.findViewById(R.id.name);
zPhone = (TextView) itemView.findViewById(R.id.phone);
zEmail = (TextView) itemView.findViewById(R.id.email);
zWebsite = (TextView) itemView.findViewById(R.id.website);
zLocation = (TextView) itemView.findViewById(R.id.location);
zCategory = (TextView) itemView.findViewById(R.id.category);
}
}
public RecyclerAdapter(Context context,List<Businesses> data)
{
inflater=LayoutInflater.from(context);
this.data= data;
}
#Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View recView = inflater.inflate(R.layout.recycler_layout, parent, false);
ViewHolder ViewHolder = new ViewHolder(recView);
return ViewHolder;
}
#Override
public void onBindViewHolder(RecyclerAdapter.ViewHolder viewHolder, int position)
{
Businesses businesses = data.get(position);
TextView name = viewHolder.zName;
name.setText(businesses.getName());
TextView phone = viewHolder.zPhone;
phone.setText(businesses.getPhone());
TextView email = viewHolder.zEmail;
email.setText(businesses.getEmail());
TextView website = viewHolder.zWebsite;
website.setText(businesses.getWebsite());
TextView location = viewHolder.zLocation;
location.setText(businesses.getLocation());
TextView category = viewHolder.zCategory;
category.setText(businesses.getCategory());
}
#Override
public int getItemCount()
{
return data.size();
}
}
Categories(Fragment)
public class Categories extends Fragment
{
List<Businesses> data = new ArrayList<>();
private RecyclerView zRecyclerView;
private RecyclerAdapter zAdapter;
private RecyclerView.LayoutManager zLayoutManager;
public Categories()
{
// Required empty public constructor
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_categories,container, false);
zRecyclerView = (RecyclerView) rootView.findViewById(R.id.recview_categories);
zRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
getData();
return super.onCreateView(inflater, container, savedInstanceState);
}
public void getData()
{
final List<Businesses> data = new ArrayList<>();
ParseQuery<Businesses> query = new ParseQuery<>("Businesses");
query.findInBackground(new FindCallback<Businesses>()
{
#Override
public void done(List<Businesses> list, ParseException e)
{
if(e == null)
{
for(Businesses businesses : list)
{
Businesses bizList = new Businesses();
bizList.setNames(businesses.getNames());
bizList.setPhone(businesses.getPhone());
bizList.setEmail(businesses.getEmail());
bizList.setWebsite(businesses.getWebsite());
bizList.setLocation(businesses.getLocation());
bizList.setCategory(businesses.getCategory());
data.add(bizList);
}
zAdapter = new RecyclerAdapter(getActivity(), data);
zRecyclerView.setAdapter(zAdapter);
}
}
});
}
}
Everything looks good. You just have to call notifyDataSetChanged() on zAdapter whenever you are changing the data. So change your code like this:
zAdapter = new RecyclerAdapter(getActivity(), data);
zRecyclerView.setAdapter(zAdapter);
zAdapter.notifyDataSetChanged();