FirebaseInstanceId: Token retrieval failed android - android

I am trying to retrive data using a recylerView, but I keep getting an error in regards to this line Upload upload=postsnapshot.getValue(Upload.class); but it's not telling me what the problem is exaclty and plus I get another error saying this E/FirebaseInstanceId: Token retrieval failed: INVALID_SENDER I'm not sure what's going on. Here is my code below. Any help will be appricated. Thanks in advance
//Log cat error
2020-03-31 17:15:47.842 1943-1943/? E/FirebaseInstanceId: binding to the service failed
//This my model class
public class Upload {
private String mImageUrl;
private String nickName;
private String fullname;
private String age;
private String height;
public Upload(){
//Empty constructor needed
}
public Upload(String imageUrl ){
mImageUrl=imageUrl;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public String getmImageUrl() {
return mImageUrl;
}
public void setmImageUrl(String mImageUrl) {
this.mImageUrl = mImageUrl;
}
//My adapter class
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder>{
private Context mContext;
private List<Upload> mUploads;
public ImageAdapter(Context context, List<Upload> uploads){
mContext = context;
mUploads = uploads;
}
#NonNull
#Override
public ImageViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View V = LayoutInflater.from(mContext).inflate(R.layout.cardview, parent, false);
return new ImageViewHolder(V);
}
#Override
public void onBindViewHolder(#NonNull ImageViewHolder holder, int position) {
Upload uploadCurrent=mUploads.get(position);
holder.txt1.setText(uploadCurrent.getnickName());
Picasso.get().load(String.valueOf(uploadCurrent)).fit().centerCrop().into(holder.imageView);
}
#Override
public int getItemCount() {
return mUploads.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder{
public ImageView imageView;
public TextView txt1;
public ImageViewHolder(#NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.imageview);
txt1=itemView.findViewById(R.id.nickname);
}
}
//This the main page
databaseReference.child(uid).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
Upload upload=postsnapshot.getValue(Upload.class);
mUploads.add(upload);
}
mAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(MainActivity.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
//This class is where they upload their image
storageRef.putFile(filepath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//Toast.makeText(UploadAdActivity.this,"Uploaded successfully",Toast.LENGTH_LONG).show();
//Upload upload=new Upload(taskSnapshot.getStorage().getDownloadUrl().toString());
//String uploadId=update.push().getKey();
//update.child(uploadId).setValue(upload);
progressDialog.dismiss();
Toast.makeText(UploadAdActivity.this,"Uploaded successfully",Toast.LENGTH_LONG).show();
storageReference.getDownloadUrl().toString();
taskSnapshot.getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
String download_url=uri.toString();
Upload upload=new Upload(download_url);
String uploadId=update.push().getKey();
// String a=update.push().getKey();
Intent d=new Intent(UploadAdActivity.this,AllInOneActivity.class);
d.putExtra("a",uploadId);
update.child(uploadId).setValue(upload);
Intent i = new Intent(UploadAdActivity.this, NextPage.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.putExtra("a",uploadId);
startActivity(i);
}
});
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
progressDialog.dismiss();
Toast.makeText(UploadAdActivity.this,"Failed to upload",Toast.LENGTH_LONG).show();
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
#Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0*taskSnapshot.getBytesTransferred()/taskSnapshot
.getTotalByteCount());
progressDialog.setMessage("Uploaded "+(int)progress+"%");
}
});

Related

Firebase Database Update profile picture on every post in firebase recyclerview

I have a feed with posts from different users that is built using the FirebaseRecyclerAdapter.
It works fine but when i change a user's profile picture i want the profile picture from all of that user's posts to change as well.
At the moment it just displays the profile picture that was present at the moment the post was created.
Here is the code:
I call chooseImage when i click the upload button:
private void chooseImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
&& data != null && data.getData() != null) {
imageUri = data.getData();
uploadImage();
}
}
private void uploadImage() {
if (imageUri != null) {
StorageReference fileReference = storageReference
.child(FirebaseAuth.getInstance().getCurrentUser().getUid() + "." + getImageExtension(imageUri));
fileReference.putFile(imageUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Handler handler = new Handler();
Picasso.with(getContext()).load(imageUri).into(imgProfile);
handler.postDelayed(new Runnable() {
#Override
public void run() {
progressBar.setProgress(0);
progressBar.setVisibility(View.GONE);
}
}, 1000);
Toast.makeText(getActivity(), "Image changed successfully", Toast.LENGTH_LONG).show();
databaseReference.child("profilePic").setValue(taskSnapshot.getStorage().getDownloadUrl().toString());
UserProfileChangeRequest profileChangeRequest = new UserProfileChangeRequest.Builder()
.setPhotoUri(imageUri)
.build();
user.updateProfile(profileChangeRequest);
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
#Override
public void onProgress(#NonNull UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress((int) progress);
}
});
} else {
Toast.makeText(this.getContext(), "No image selected", Toast.LENGTH_SHORT).show();
}
}
This is how i add the posts:
btnPostComm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String story = et.getText().toString();
String title = et2.getText().toString();
String key = refference.push().getKey();
String name = user.getDisplayName();
String uri = user.getPhotoUrl().toString();
CommModel model = new CommModel(
story,
title,
name,
uri
);
refference
.child(key)
.setValue(model);
}
});
This is my Model class:
public class CommModel {
private String story;
private String title;
private String name;
private String uri;
public CommModel(){}
public CommModel(String story, String title, String name, String uri) {
this.story = story;
this.title = title;
this.name = name;
this.uri = uri;
}
public String getStory() {
return story;
}
public void setStory(String story) {
this.story = story;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
}
This is the ViewHolder:
public class PostsViewHolder extends RecyclerView.ViewHolder {
public TextView txtStory;
public TextView txtTitle;
public TextView txtName;
public CircularImageView proImage;
public QNAViewHolder(#NonNull View itemView) {
super(itemView);
txtStory = itemView.findViewById(R.id.txtStory);
txtTitle = itemView.findViewById(R.id.txtTitle);
txtName = itemView.findViewById(R.id.txtName1);
proImage = itemView.findViewById(R.id.profile3);
}
}
And this is the Adapter in the Fragment that hosts the RecyclerView:
public void showPosts() {
FirebaseRecyclerOptions options = new FirebaseRecyclerOptions.Builder<CommModel>()
.setQuery(ref, CommModel.class)
.build();
adpt = new FirebaseRecyclerAdapter<CommModel, PostsViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull PostsViewHolder holder, int position, #NonNull CommModel model) {
holder.txtStory.setText(model.getStory());
holder.txtTitle.setText(model.getTitle());
holder.txtName.setText(model.getName());
Glide.with(Posts.this).load(model.getUri()).into(holder.proImage);
}
#Override
public int getItemCount() {
return super.getItemCount();
}
#Override
public void onDataChanged() {
recyclerView.removeAllViews();
super.onDataChanged();
}
#NonNull
#Override
public CommModel getItem(int position) {
return super.getItem(getItemCount() - 1 - position);
}
#NonNull
#Override
public PostsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.qna_feed, parent, false);
return new PostsViewHolder(view);
}
};
adpt.startListening();
adpt.notifyDataSetChanged();
recyclerView.setAdapter(adpt);
}
How can i update the pictures on every post of a user when he changes his profile pic?
Thank you very much!
If you want to do that, you have to change the CommModel class. You need to add another property, such as userUid.
private String story;
private String title;
private String name;
private String uri;
private String userUid; //Create new one and getter setter too
After that, at your showPosts method, you can call the user's property and get his profile picture.
holder.txtStory.setText(model.getStory());
holder.txtTitle.setText(model.getTitle());
holder.txtName.setText(model.getName());
final String userUid = model.getUserUid();
//Call Firebase to get user current profile.
FirebaseDatabase.getInstance().getReference().child("User").child(userUid).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exist()){
final String pictureUrl = dataSnapshot.chilld("profilePic").getValue(String.class);
Glide.with(Posts.this).load(pictureUrl).into(holder.proImage);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

Users information not displaying in recyclerview

I'm working on this project where users are able to upload more than one character image along with other information. Such as the name of the character, what the character will do, the color of the character and its height. Now everything saves successfully in the database. However, when I want to display all of the character's information on the user's profile page, using a recyclerView, the saved information in the database will not display on the user's profile page. Below is my code. Thanks in advance
//Users profile page
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
overridePendingTransition(R.anim.slide_right, R.anim.slide_left);
mUploads = new ArrayList<>();
mAdapter = new ImageAdapter(getApplicationContext(), mUploads);
recyclerView = findViewById(R.id.recyclerView);
//recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
if (firebaseAuth != null) {
databaseReference = FirebaseDatabase.getInstance().getReference("Users");
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
firebaseStorage = FirebaseStorage.getInstance();
storageReference = firebaseStorage.getReference();
//upload = findViewById(R.id.button_upload);
//button2 = findViewById(R.id.submit);
//imageView = findViewById(R.id.view_image);
//upload.setOnClickListener(this);
databaseReference.child(uid).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
String imageUrl = (String) postsnapshot.child("mImageUrl").getValue();
mUploads.add(imageUrl);
//mUploads = new ArrayList<>();
}
mAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(ProfileActivity.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
//Model Upload.class
public class Upload {
private String mImageUrl;
public Upload(){
//Empty constructor needed
}
public Upload (String imageUrl){
mImageUrl=imageUrl;
}
public String getmImageUrl() {
return mImageUrl;
}
public void setmImageUrl(String mImageUrl) {
this.mImageUrl = mImageUrl;
}
}
// Image adapter class
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder>{
private Context mContext;
private List<String> mUploads;
public ImageAdapter(Context context, List<String> uploads){
mContext = context;
mUploads = uploads;
}
#NonNull
#Override
public ImageViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View V = LayoutInflater.from(mContext).inflate(R.layout.cardview, parent, false);
return new ImageViewHolder(V);
}
#Override
public void onBindViewHolder(#NonNull ImageViewHolder holder, int position) {
String uploadCurrent=mUploads.get(position);
Picasso.get().load(uploadCurrent).fit().centerCrop().into(holder.imageView);
}
#Override
public int getItemCount() {
return mUploads.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder{
public ImageView imageView;
public ImageViewHolder(#NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.imageview);
}
}
}
Change this:
databaseReference.child(uid).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
String imageUrl = (String) postsnapshot.child("mImageUrl").getValue();
mUploads.add(imageUrl);
//mUploads = new ArrayList<>();
}
mAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(ProfileActivity.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
into this:
databaseReference.child(uid).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
String imageUrl = postsnapshot.child("mImageUrl").getValue(String.class);
mUploads.add(imageUrl);
//mUploads = new ArrayList<>();
mAdapter.notifyDataSetChanged();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(ProfileActivity.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
If you want to read inside the children of children, you need to do double loop like this.
databaseReference.child(uid).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
for (DataSnapshot snapshot : postsnapshot.getChildren()) {
String imageUrl = snapshot.child("mImageUrl").getValue(String.class);
mUploads.add(imageUrl);
//mUploads = new ArrayList<>();
}
}
mAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(ProfileActivity.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
Edited:
If you want to pass all of them, you need to change a bit of your model class
public class Upload {
private String mImageUrl;
private String Character_Name;
private String What_Character_Does;
private String color;
private String height;
public Upload() {
//Empty constructor needed
}
public Upload(String imageUrl) {
mImageUrl = imageUrl;
}
public String getmImageUrl() {
return mImageUrl;
}
public void setmImageUrl(String mImageUrl) {
this.mImageUrl = mImageUrl;
}
public String getCharacter_Name() {
return Character_Name;
}
public void setCharacter_Name(String character_Name) {
Character_Name = character_Name;
}
public String getWhat_Character_Does() {
return What_Character_Does;
}
public void setWhat_Character_Does(String what_Character_Does) {
What_Character_Does = what_Character_Does;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
}

How can i fill the recylerview with firebase realtime database data?

I tried so many solutions about this, but none of this resolve my problem. I can not reach the database using recyclerview. I have firebase realtime database data like this;
Tag
--- Main Category
----- keyID
------ main-Category, sub-category
Tags class
public class Tags {
private String id;
private String mainTags;
private String subTags;
public Tags() {}
public Tags(String id, String mainTags, String subTags) {
this.id = id;
this.mainTags = mainTags;
this.subTags = subTags;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMainTags() {
return mainTags;
}
public void setMainTags(String mainTags) {
this.mainTags = mainTags;
}
public String getSubTags() {
return subTags;
}
public void setSubTags(String subTags) {
this.subTags = subTags;
}
}
and this is TagAdapter
public class TagAdapter extends RecyclerView.Adapter<TagAdapter.ItemViewHolder> {
private List<Tags> mTagList;
private Context mContext;
public TagAdapter(List<Tags> mTagList, Context mContext) {
this.mTagList = mTagList;
this.mContext = mContext;
}
#NonNull
#Override
public ItemViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.each_item_tags, parent,false);
return new ItemViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ItemViewHolder holder, int position) {
Tags tags = mTagList.get(position);
holder.tvTags.setText(tags.getSubTags());
holder.tvMainTags.setText(tags.getMainTags());
}
#Override
public int getItemCount() {
return mTagList.size();
}
public class ItemViewHolder extends RecyclerView.ViewHolder{
private TextView tvMainTags, tvTags;
public ItemViewHolder(#NonNull View itemView) {
super(itemView);
tvMainTags = itemView.findViewById(R.id.tvMainTag);
tvTags = itemView.findViewById(R.id.tvTags);
}
}
}
TagsFragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_tags, container, false);
rvTags = view.findViewById(R.id.rvTags);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rvTags.setLayoutManager(linearLayoutManager);
rvTags.hasFixedSize();
tagAdapter = new TagAdapter(tagsList, getContext());
rvTags.setAdapter(tagAdapter);
return view;
}
private void getTagView(){
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference dbRefTags = firebaseDatabase.getReference().child("Tag");
dbRefTags.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
for (DataSnapshot ds : dataSnapshot.getChildren()){
if (ds.exists()) {
try {
Tags tags = ds.getValue(Tags.class);
tagsList.add(tags);
}catch (Exception e){
Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}else {
Toast.makeText(getContext(), "Bir hata oluştu", Toast.LENGTH_SHORT).show();
}
}
tagAdapter.notifyDataSetChanged();
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
There is no error show up when running but i couldn't be able to retrieve data. And i tried firebaseRecyclerView.Adapter also and in the last dependency, i couldn't make it work. I don't know what is wrong in here. how can i solve this?
Get The data with child name ds.child("SubTag").getValue()
dbRefTags.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot parentDS : dataSnapshot.getChildren()) {
Log.d("Tag:", String.valueOf(parentDS.getKey()));
for (DataSnapshot ds : parentDS.getChildren()) {
Tags tags = new Tags();
tags.setMainTag(parentDS.getKey());
tags.setSubTag(ds.child("SubTag").getValue().toString());
Log.d("Tag: -> SubTag)", tags.getSubTag());
tagsList.add(tags);
}
}
tagAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(getContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});

How to get Particular Child nodes in Fire base android?

i need to get bestseller values from AGS Biryani in recyler view , No idea about how to add the child values to the recyler adapter based on parent node
Model
public class Food_List {
private String itemname;
private String itemdescrp;
private long itemnewprice;
private long itemoldprice;
public String getItemname() {
return itemname;
}
public void setItemname(String itemname) {
this.itemname = itemname;
}
public String getItemdescrp() {
return itemdescrp;
}
public void setItemdescrp(String itemdescrp) {
this.itemdescrp = itemdescrp;
}
public long getItemnewprice() {
return itemnewprice;
}
public void setItemnewprice(long itemnewprice) {
this.itemnewprice = itemnewprice;
}
public long getItemoldprice() {
return itemoldprice;
}
public void setItemoldprice(long itemoldprice) {
this.itemoldprice = itemoldprice;
}
}
Food_List_ViewHolders
ViewHolders to add the value in Recyler view
public class Food_List_ViewHolders extends RecyclerView.ViewHolder {
View mView;
private Context context;
Context mContext;
String nodata;
public Food_List_ViewHolders(View itemView) {
super(itemView);
mView=itemView;
}
#SuppressLint("SetTextI18n")
public void setDetails(Context applicationContext, final String itemname, String itemdescrp,
final long itemnewprice,
final long itemoldprice
)
{
final TextView dishitemname=mView.findViewById(R.id.dishheader);
TextView dishitemnamedescrp=mView.findViewById(R.id.dishheaderdescrp);
TextView dishitemnameoldprice=mView.findViewById(R.id.itemoldprice);
TextView dishitemnamenewprice=mView.findViewById(R.id.itemnewprice);
dishitemname.setText(itemname);
dishitemnamedescrp.setText(itemdescrp);
dishitemnamenewprice.setText(applicationContext.getString(R.string.Rup) + itemnewprice);
dishitemnameoldprice.setText(applicationContext.getString(R.string.Rup) + itemoldprice);
mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View mView) {
Context context = mView.getContext();
}
});
}
}
Main Activity
Don't Know how to add Child values in DataSnapshot
mRecycleriew =findViewById(R.id.my_recycler_views);
mRecycleriew.setLayoutManager(new LinearLayoutManager(this));
mFirebaseDatabase= FirebaseDatabase.getInstance();
mRef=mFirebaseDatabase.getReference().child("restaurants").equalTo("AGS Biryani");
//DatabaseReference restaurantsRef = mRef.child("restaurants");
mRef(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()){
progressDoalog.dismiss();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
private void mRef(ValueEventListener valueEventListener) {
}
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Food_List,Food_List_ViewHolders> firebaseRecyclerAdapter=
new FirebaseRecyclerAdapter<Food_List, Food_List_ViewHolders>(
Food_List.class,
R.layout.item_child,
Food_List_ViewHolders.class,
mRef)
{
#Override
protected void populateViewHolder(Food_List_ViewHolders viewHolder, Food_List model, int position) {
viewHolder.setDetails(getApplicationContext(),model.getItemname(),model.getItemdescrp(),model.getItemnewprice(),model.getItemoldprice());
}
};
mRecycleriew.setAdapter(firebaseRecyclerAdapter);
}
Essentially you're doing:
mRef=mFirebaseDatabase.getReference().child("restaurants").equalTo("AGS Biryani");
FirebaseRecyclerAdapter<Food_List,Food_List_ViewHolders> firebaseRecyclerAdapter=
new FirebaseRecyclerAdapter<Food_List, Food_List_ViewHolders>(
Food_List.class,
R.layout.item_child,
Food_List_ViewHolders.class,
mRef)
Which means that you're showing all restaurants with a priority of AGS Biryani. That's not what you're trying to do, so you'll need to modify your ref:
mRef=mFirebaseDatabase.getReference().child("restaurants/AGS Biryani/bestsellers");
When you pass this ref into the FirebaseRecyclerAdapter, it will show all bestsellers for AGS Biryani.

Items are added in RecyclerView but not visible

I'm retrieving list of few users which being added and but are not visible. When I added Toast on item click it shows data null and android studio give warning also in model class the constructor is never used .I'm using ChildEventListener and a custom adapter
Custom adapter code
class CustomMemberAdapter extends RecyclerView.Adapter<SetMeeting_2.User_MemberHolder>{
List<UserModel_member_DP_NAME> mList;
CustomMemberAdapter(List<UserModel_member_DP_NAME> mList) {
this.mList = mList;
}
#Override
public User_MemberHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.set_meeting_userlist_item,parent,false);
return new User_MemberHolder(v);
}
#Override
public void onBindViewHolder(User_MemberHolder holder, int position) {
final UserModel_member_DP_NAME model = mList.get(position);
holder.txt.setText(model.getName());
Picasso.with(holder.itemView.getContext()).load(model.getDp()).into(holder.img);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(view.getContext(), String.valueOf(model.getName()), Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return mList.size();
}
}
Model Class code
private static class UserModel_member_DP_NAME {
String dp,name;
UserModel_member_DP_NAME(){}
UserModel_member_DP_NAME(String dp, String name) { //never used warning
this.dp = dp;
this.name = name;
}
public String getDp() {
return dp;
}
public void setDp(String dp) {
this.dp = dp;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
ChildEventListener code
mDatabase = FirebaseDatabase.getInstance().getReference();
defaultQuery = mDatabase.child("users").child(current_user).child("directorylist");
defaultQuery.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
UserModel_member_DP_NAME model = dataSnapshot.getValue(UserModel_member_DP_NAME.class);
memberList.add(model);
adapter.notifyDataSetChanged();
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
recyclerView.setAdapter(adapter);
here is my database structure

Categories

Resources