Why is my data coming back as a null from firebase? - android

I am trying to retrieve some data from Firebase to populate into my RecycleView but every time I attempt this my RecycleView appears empty.
I have already created my adapter and set up my Recycleview and attempted to load the data from Firebase but nothing shows up. When I try to log the information it comes back as null. Some help would be appreciated.
This is how my database is set up
Post
-Ljl__LUiOJ2YMAK5NHI
desc: "Product info"
id: "sH8LzoaH9UaXahlmssixTpvQy8q2"
image: "content://media/external/images/media/323211"
//Retrieves information stored inside Post node...
public void fetchUserInfo() {
postRef = FirebaseDatabase.getInstance().getReference().child("Post");
postRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
value = ds.getValue(Post.class);
postList.add(value);
}
adapter = new Adapter(Shop_Activity.this, postList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.i("Error", databaseError.toString());
}
});
}
Adapter :
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
Context context;
ArrayList<Post> userPost;
public Adapter(Context context, ArrayList<Post> userPost){
this.context = context;
this.userPost = userPost;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.shop_layout_design,viewGroup, false));
}
//this is where you set the value for the ui elements
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int i) {
viewHolder.title.setText(userPost.get(i).getDescription());
viewHolder.description.setText(userPost.get(i).getUserID());
Picasso.get().load(userPost.get(i).getPostImage()).into(viewHolder.postImage);
}
#Override
public int getItemCount() {
return userPost.size();
}
//links up ui elements
class ViewHolder extends RecyclerView.ViewHolder{
TextView title;
TextView description;
ImageView postImage;
public ViewHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.post_title);
description = itemView.findViewById(R.id.post_desc);
postImage = itemView.findViewById(R.id.post_image);
}
}
}
Post class
public class Post {
private String desc;
private String id;
private String image;
public Post(){
}
public void setdesc(String desc){
this.desc = desc;
}
public void setimage(String image){
this.image = image;
}
public void setid(String id){
this.id = id;
}
public String getdesc() {
return desc;
}
public String getimage(){
return image;
}
public String getid(){
return id;
}
}

adapter.notifyDataSetChanged();
add this after setting the adapter

Related

How to show only those records (from record 1) in an Adapter which are set as 'true' (in record 2) in Realtime Database?

I am trying to fetch and populate my Adapter with only those records which are set as true (in a record), and if they are false, I would not like to fetch and populate my Adapter with them.
I am basically trying to build a Train Pantry Food Ordering System in which Passengers can order food from Pantry car. But with one condition :
Food should be available in the particular train's Pantry car in which the Passenger is travelling.
Here is my Realtime Database :
Here, Firstly I want to fetch the boolean values for entered train number from Catalog -> (trainno) -> data(true/false), then accordingly, if it is true, I want to populate my Adapter with those food items only while getting food-item data from foodinfo -> (foodname) -> data.
Here are my codes :
Food :
public class Food {
private String description;
private String imageurl;
private String foodname;
public Food(String description, String imageurl, String foodname) {
this.description = description;
this.imageurl = imageurl;
this.foodname = foodname;
}
public Food(){}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImageurl() {
return imageurl;
}
public void setImageurl(String imageurl) {
this.imageurl = imageurl;
}
public String getFoodname() {
return foodname;
}
public void setFoodname(String foodname) {
this.foodname = foodname;
}
}
FoodAdapter:
public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.Viewholder>{
private final Context mContext;
private final List<Food> mFoods;
public FoodAdapter(Context mContext, List<Food> mFoods) {
this.mContext = mContext;
this.mFoods = mFoods;
}
#NonNull
#Override
public Viewholder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.food_item, parent, false);
return new FoodAdapter.Viewholder(view);
}
#Override
public void onBindViewHolder(#NonNull Viewholder holder, int position) {
Food food = mFoods.get(position);
holder.foodDescription.setText(food.getDescription());
Picasso.get().load(food.getImageurl()).into(holder.foodImage);
holder.foodName.setText(food.getFoodname());
FirebaseDatabase.getInstance().getReference().child("foodinfo").child(food.getFoodname())
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
Food food1 = dataSnapshot.getValue(Food.class);
holder.foodName.setText(food1.getFoodname());
holder.foodDescription.setText(food1.getDescription());
Picasso.get().load(food.getImageurl()).into(holder.foodImage);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
#Override
public int getItemCount() {
return mFoods.size();
}
public class Viewholder extends RecyclerView.ViewHolder {
public ImageView foodImage;
public TextView foodName;
public TextView foodDescription;
public Viewholder(#NonNull View itemView) {
super(itemView);
foodImage =itemView.findViewById(R.id.food_image);
foodName = itemView.findViewById(R.id.food_name);
foodDescription = itemView.findViewById(R.id.food_description);
}
}
}
HomeFragment:
public class HomeFragment extends Fragment {
private RecyclerView recyclerViewMenu;
private FoodAdapter foodAdapter;
private List<Food> foodList;
private List<String> availableFoodsList; // foods available for a train passenger only that will be shown.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
recyclerViewMenu = view.findViewById(R.id.recycler_view_foods);
recyclerViewMenu.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setStackFromEnd(true);
linearLayoutManager.setReverseLayout(true);
recyclerViewMenu.setLayoutManager(linearLayoutManager);
foodList = new ArrayList<>();
foodAdapter = new FoodAdapter(getContext(), foodList);
recyclerViewMenu.setAdapter(foodAdapter);
availableFoodsList = new ArrayList<>();
return view;
}
}
you should modify the underlayer data (foodList) then use Adapter.notifyDataSetChanged() to refresh the recycler view for any adapter data changes

open webview in new activity when item is clicked

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.

how displaying server information in Recyclerview?

I want to write a program to retrieve information from the server and display it in the Recycler View,But I have two problems.
When I add data to the table it will be added to the Recycler View list But when I delete, the list doesn't change.
2.Photos of each section cannot be loaded.
home.java
public class Home extends Fragment {
public Home() {
// Required empty public constructor
}
SwipeRefreshLayout srl;
RecyclerView rv;
FloatingActionButton add;
ArrayList<Post> al;
PostAdapter pa;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
srl = getActivity().findViewById(R.id.srl);
rv = getActivity().findViewById(R.id.rv);
// add = getActivity().findViewById(R.id.add);
AndroidNetworking.initialize(getContext());
al = new ArrayList<>();
pa = new PostAdapter(getContext(),al);
rv.setHasFixedSize(true);
rv.setItemAnimator(new DefaultItemAnimator());
rv.setLayoutManager(new LinearLayoutManager(getContext()));
rv.setAdapter(pa);
update();
srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
update();
}
});
}
private void update(){
String id = al.size() == 0 ? "0" : al.get(0).getId();
AndroidNetworking.post(Urls.host+Urls.post)
.addBodyParameter("id",id)
.build()
.getAsObjectList(Post.class, new ParsedRequestListener<List<Post>>() {
#Override
public void onResponse(List<Post> response) {
srl.setRefreshing(false);
for (Post p : response){
al.add(0,p);
pa.notifyDataSetChanged();
}
}
#Override
public void onError(ANError anError) {
srl.setRefreshing(false);
}
});
}
}
postadapter.java
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {
Context context;
ArrayList<Post> al;
LayoutInflater inflater;
public PostAdapter(Context ctx,ArrayList<Post> arl){
context = ctx;
al = arl;
inflater = LayoutInflater.from(context);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.rv_item,null,false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Post s = al.get(position);
holder.title.setText(s.getTitle());
holder.text.setText(s.getText());
holder.date.setText(s.getDate());
holder.image.setImageUrl(Urls.host+s.getImage());
holder.image.setDefaultImageResId(R.drawable.material);
holder.image.setErrorImageResId(R.drawable.material);
}
#Override
public int getItemCount() {
return al.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
ANImageView image;
AppCompatTextView title, text, date;
AppCompatImageView share;
public ViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.image);
title = itemView.findViewById(R.id.title);
text = itemView.findViewById(R.id.text);
date = itemView.findViewById(R.id.date);
}
}
}
post.java
public class Post {
String title,text, date,id;
int imageUrl;
public Post(int imageUrl, String title, String text, String time, String id) {
this.imageUrl = imageUrl;
this.title = title;
this.text = text;
this.date = time;
this.id = id;
}
public int getImage() {
return imageUrl;
}
public void setImage(int imageUrl) {
this.imageUrl = imageUrl;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
Try this
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {
Context context;
ArrayList<Post> al;
ArrayList<Post> newDataList;
LayoutInflater inflater;
public PostAdapter(Context ctx,ArrayList<Post> arl){
context = ctx;
al = arl;
inflater = LayoutInflater.from(context);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.rv_item,null,false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Post s = al.get(position);
holder.title.setText(s.getTitle());
holder.text.setText(s.getText());
holder.date.setText(s.getDate());
holder.image.setImageUrl(Urls.host+s.getImage());
holder.image.setDefaultImageResId(R.drawable.material);
holder.image.setErrorImageResId(R.drawable.material);
}
#Override
public int getItemCount() {
return al.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
ANImageView image;
AppCompatTextView title, text, date;
AppCompatImageView share;
public ViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.image);
title = itemView.findViewById(R.id.title);
text = itemView.findViewById(R.id.text);
date = itemView.findViewById(R.id.date);
}
public ArrayList<Post> getDataSet() {
return al;
}
public void refresh(ArrayList<Post> list) {
newDataList = new ArrayList<>();
newDataList.addAll(list);
al.clear();
al.addAll(newDataList);
notifyDataSetChanged();
}
}
Now at time when you wants to add or delete try getting adapter.getDataSet()
and update list with adapter.refresh(yourList)

Recyclerview is not showing any data which i have in firebase. Iam able to retrieve the data

HISTOROBJECT.CLASS
public class HistoryObject{
private String mName;
private String mImageUrl;
public HistoryObject(){
}
public HistoryObject(String Name, String Image) {
this.user = Name;
this.msg = Image;
}
private String user, msg;
public String getMsg() {
return msg;
}
public void setMsg(String Name) {
this.msg = Name;
}
public String getUser() {
return user;
}
public void setUser(String Image) {
this.user = Image;
}
}
HISTORYADAPTER.CLASS
public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ImageViewHolder> {
private Context mContext;
private List<HistoryObject> mUploads;
public HistoryAdapter (Context cx, List<HistoryObject> uploads){
mContext = cx;
mUploads = uploads;
}
#NonNull
#Override
public ImageViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.listofitems, parent, false);
return new ImageViewHolder(v); }
#Override
public void onBindViewHolder(#NonNull ImageViewHolder holder, int position) {
HistoryObject historyObject = mUploads.get(position);
holder.textView.setText(historyObject.getUser());
Picasso.get().load(historyObject.getMsg())
.placeholder(R.mipmap.ic_launcher)
.fit()
.centerCrop()
.into(holder.spapic);
}
#Override
public int getItemCount() {
return mUploads.size() ;
}
public class ImageViewHolder extends RecyclerView.ViewHolder{
public TextView textView;
public ImageView spapic;
public ImageViewHolder(View itemView) {
super(itemView);
textView =(TextView)itemView.findViewById(R.id.nameofspa);
spapic =(ImageView) itemView.findViewById(R.id.spapic);
}
}
}
HOMEPAGE.CLASS
mDatabaseRef = FirebaseDatabase.getInstance().getReference("BeautyCenters");
mDatabaseRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
// Log.d("SPAA", "VALUE IS " +dataSnapshot) ;
Log.d("SPAA", "postsnap IS " +postSnapshot) ;
HistoryObject upload = postSnapshot.getValue(HistoryObject.class);
mUploads.add(upload);
}
mHistoryAdapter = new HistoryAdapter(HomePage.this, mUploads);
mHistoryRecyclerView.setAdapter(mHistoryAdapter);
mHistoryAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Iam able to retrieve data and can see all list in logcat but it is not displaying in recyclerview. I also want to retrieve the images from the firebase. iam able to show the adapter but the text is not showing in that adapter instead it is blank and also the picture
I have faced this problem before.
Try moving:
mHistoryRecyclerView.setAdapter(mHistoryAdapter);
mHistoryAdapter.notifyDataSetChanged();
out of the listener class to the bottom of onCreate.
If that doesn't work make sure you have this code added:
#Override
public void onStart() {
super.onStart();
if (mAdapter != null) {
mAdapter.startListening();
}
}
If it is still not working then provide a link of your project and I will play around with it.

Picasso failed to setImage on CircleImageView on a recyclerview

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

Categories

Resources