Glide is not loading image from firebase - java

I am working on a firebase project but I am facing a problem like when I upload an image on firebase I am not getting it in my original apk. I upload an image on firebase and save its URL in the firebase database and then when I load it with the Recycler view and Glide library it stuck on the placeholder image but did not load the original image. Can anyone help me to solve this problem I provided my source code below:-
EventActivity:-
public class EventActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private ImageAdapter imageAdapter;
private DatabaseReference mDatabaseRef;
private List<Upload> mUploads;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event);
mDatabaseRef = FirebaseDatabase.getInstance().getReference("NotifyImages");
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mUploads = new ArrayList<>();
mDatabaseRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Upload upload = postSnapshot.getValue(Upload.class);
mUploads.add(upload);
}
imageAdapter = new ImageAdapter(EventActivity.this, mUploads);
mRecyclerView.setAdapter(imageAdapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
Upload class:-
public class Upload {
private String mName;
private String mImageUri;
public Upload() {
}
public Upload(String name, String ImageUri) {
if (name.trim().equals("")) {
name = "No name";
}
mName = name
;
mImageUri = ImageUri;
}
public String getName() {
return mName;
}
public void setName(String name) {
mName = name;
}
public String getImageUri() {
return mImageUri;
}
public void setImageUri(String ImageUri) {
mImageUri = ImageUri;
}
}
Image Adapter:-
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.image_item, parent, false);
return new ImageViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull ImageViewHolder holder, int position) {
Upload uploadCurrent = mUploads.get(position);
holder.textViewName.setText(uploadCurrent.getName());
// Glide.with(mContext)
// .load(uploadCurrent.getImageUri())
// .centerCrop()
// .into(holder.imageView);
Glide.with(mContext)
.load(uploadCurrent.getImageUri())
.placeholder(R.mipmap.ic_launcher_round)
.centerCrop()
.into(holder.imageView);
}
#Override
public int getItemCount() {
return mUploads.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder {
public TextView textViewName;
public ImageView imageView;
public ImageViewHolder(#NonNull View itemView) {
super(itemView);
textViewName = itemView.findViewById(R.id.text_view_name);
imageView = itemView.findViewById(R.id.image_upload);
}
}
}
activity_event.xml:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
ImageItem.xml:-
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/text_view_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="8dp"
android:layout_marginBottom="80dp"
android:text="Name"
android:textColor="#color/ColorRed"
android:textSize="20sp" />
<ImageView
android:id="#+id/image_upload"
android:layout_width="match_parent"
android:layout_height="300dp"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
You can easily copy my code and test it. Please answer me if you find a solution.

Please make changes in your UploadFile() Function :
fireRef.putFile(ImageUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
}
} , 5000);
Toast.makeText(MainActivity.this, "Upload Successfull", Toast.LENGTH_SHORT).show();
Task<Uri> urlTask = taskSnapshot.getStorage().getDownloadUrl();
while (!urlTask.isSuccessful());
Uri downloadUrl = urlTask.getResult();
Upload upload = new Upload(mainEditTextNotify.getText().toString().trim(),
downloadUrl.toString());
String uploadId = mDataRef.push().getKey();
mDataRef.child(uploadId).setValue(upload);
}
})
The above code will provide you proper uri of image which you can store inside Database.
It will provide you proper uri which will help to load images in Glide.
But i suggest to store filepath from Firebase Storage. Cause if uri token expires then you need generate uri token and change with old one.

Related

How to download a image from url in recylerview?

I have made a meme app, which fetches image url from an API and i m parsing them and showing in recyler view using Glide , in my item layout i have two buttons one for sharing and one for download Image , i want when user clicks the download button the images get download to the user phone.
Help me how i can implement this.
MyAdapter class
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
Context context;
ArrayList<Model> modelArrayList;
public Adapter(Context context, ArrayList<Model> modelArrayList) {
this.context = context;
this.modelArrayList = modelArrayList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_layout, parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
String url = modelArrayList.get(position).getUrl();
holder.setImage(url);
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent sharing = new Intent (Intent.ACTION_SEND);
sharing.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sharing.setType("text/plain");
String subject = "Hey Man just look at this coll meme click the link " +url;
sharing.putExtra(Intent.EXTRA_TEXT,subject);
context.startActivity(Intent.createChooser(sharing,"Shring using"));
}
});
holder.buttonDownload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
#Override
public int getItemCount() {
return modelArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
Button button,buttonDownload;
public ViewHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
button = itemView.findViewById(R.id.button);
buttonDownload = itemView.findViewById(R.id.btn_download);
}
void setImage(String link){
Glide.with(context).load(link).into(imageView);
}
}
}
My model class
public class Model {
String url;
public Model(String url) {
this.url = url;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
My item_layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="8dp"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_launcher_background"
tools:ignore="VectorDrawableCompat" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="104dp"
android:layout_marginLeft="104dp"
android:layout_marginBottom="28dp"
android:text="Share"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/btn_download"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="71dp"
android:layout_marginRight="71dp"
android:layout_marginBottom="28dp"
android:text="Download"
android:textColor="#0B0B0B"
app:backgroundTint="#12E71A"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
My mainactivity.java
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
Adapter adapter;
ArrayList<Model> arrayList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recylerview_id);
arrayList = new ArrayList<>();
String url = "https://meme-api.herokuapp.com/gimme/30";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("memes");
for (int i = 0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String url = jsonObject.getString("url");
Model m = new Model(url);
arrayList.add(m);
}
adapter = new Adapter(MainActivity.this,arrayList);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
}
});
// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);
}
}
Glide allows to load a Bitmap.
you can save Bitmap to file, and then just pass file path to another screen.
Here you can find detailed tutorial, how to store image to file with Glide library:
https://medium.com/#akshayranagujjar/how-to-save-image-to-storage-using-glide-in-android-fa26c842f212
And don't forget about storage permissions.

I retrieved some pictures and texts form the firebase into a Recycler, the texts are shown and retrieved correctly but the images are not

Here, I want to Retrieve the image and text into a RecyclerView which is the ImageActivity: here is its XML File
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ImagesActivity">
<ProgressBar
android:id="#+id/progress_circle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
The ImagesActivity Java class: Here I want to set the data into the RecyclerView using OnBindViewHolder from ImageAdapter Activity.
public class ImagesActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private ImageAdapter mAdapter;
private DatabaseReference mDatabaseRef;
private ProgressBar mProgressCircle;
private List<Upload> mUploads;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_images);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mProgressCircle = findViewById(R.id.progress_circle);
mUploads = new ArrayList<>();
mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");
mDatabaseRef.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 = new ImageAdapter(ImagesActivity.this, mUploads);
mRecyclerView.setAdapter(mAdapter);
mProgressCircle.setVisibility(View.INVISIBLE);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(ImagesActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
mProgressCircle.setVisibility(View.INVISIBLE);
}
});
}
}
ImageAdapterActiviy Java Class which retrive the data from firebase using OnBindeViewHolder:
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;
}
#Override
public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.image_item, parent, false);
return new ImageViewHolder(v);
}
#Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
Upload uploadCurrent = mUploads.get(position);
holder.textViewName.setText(uploadCurrent.getName());
Picasso.get()
.load(uploadCurrent.getImageUrl())
.fit()
.centerCrop()
.into(holder.imageView);
}
#Override
public int getItemCount() {
return mUploads.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder {
public TextView textViewName;
public ImageView imageView;
public ImageViewHolder(View itemView) {
super(itemView);
textViewName = itemView.findViewById(R.id.text_view_name);
imageView = itemView.findViewById(R.id.image_view_upload);
}
}
}
Upload Java Class: here the setter and getter method.
public class Upload {
private String mName;
private String mImageUrl;
public Upload() {
//empty constructor needed
}
public Upload(String name, String imageUrl) {
if (name.trim().equals("")) {
name = "No Name";
}
mName = name;
mImageUrl = imageUrl;
}
public String getName() {
return mName;
}
public void setName(String name) {
mName = name;
}
public String getImageUrl() {
return mImageUrl;
}
public void setImageUrl(String imageUrl) {
mImageUrl = imageUrl;
}
}

Android Firebase -Cards not displaying in RecyclerView and NullPointerException

I am trying to build an app where users can post something with image and description which is included individually in CardView in RecyclerView. So I have have got three problems
1.When id(userId) is used as child and then I post the data it gets replaced with old data instead of adding new data (if the posts has multiple children with same ID then my cards should display but the ID is getting replaced every time)
Cards aren't showing when a post is added.(When there are posts in
Firebase database)
If by default there are no posts in Firebase database
NullPointerException to the method getDesc() is generated even
though I have put null checks.
Earlier for storing posts the child I used was id(userId), but the posts got replaced instead of adding a new one so I used a random String generator and tried to fetch information from it instead.
HomeFragment.java
public class HomeFragment extends Fragment {
private RecyclerView rv;
private TextView username,date,desc;
private CircleImageView postImage;
private ImageView postBigImage;
private CardView postCard;
private List<PostActivity> postItems;
private DatabaseReference dr;
private StorageReference sr;
private FirebaseUser user;
//FirebaseRecyclerAdapter<PostActivity, userViewHolder> fra;
private PostAdapter pa;
public HomeFragment() {
// 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_home, container, false);
sr = FirebaseStorage.getInstance().getReference();
user = FirebaseAuth.getInstance().getCurrentUser();
String id = user.getUid();
AddPost ap = new AddPost();
dr =
FirebaseDatabase.getInstance().getReference().child("Posts")
.child(ap.getUni());
postItems = new ArrayList<>();
pa = new PostAdapter(postItems);
rv =view.findViewById(R.id.rvPostItems);
rv.setLayoutManager(new LinearLayoutManager(getActivity()));
rv.setAdapter(pa);
dr.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()){
//postItems.add(ds.getValue(PostActivity.class));
PostActivity pa =dataSnapshot.getValue(PostActivity.class);
postItems.add(pa);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
return view;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
PostAdapter.java
package com.pappu5.navigation;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {
public List<PostActivity> postItems;
public PostAdapter(List<PostActivity> postItems) {
this.postItems = postItems;
}
#NonNull
#Override
public PostAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_items,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final PostAdapter.ViewHolder holder, int position) {
String descTe = postItems.get(position).getDesc();
if(descTe!=null) {
holder.setDesc(descTe);
}else{
String def = "This is NULL";
holder.setDesc(def);
}
}
#Override
public int getItemCount() {
return postItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView desc;
private View mView;
public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setDesc(String descText){
desc = mView.findViewById(R.id.postDesc);
desc.setText(descText);
}
}
}
AddPost.java
package com.pappu5.navigation;
public class AddPost extends AppCompatActivity {
private Toolbar toolbar;
private ImageView postImage;
private Button postButton;
private EditText postDesc;
private static final int gallery_no = 1;
private ProgressDialog pd;
private FirebaseUser user;
private StorageReference sr;
private DatabaseReference dr;
private Uri postImageUri = null;
private Bitmap compressor;
String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
protected String getSaltString() {
String SALTCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder salt = new StringBuilder();
Random rnd = new Random();
while (salt.length() < 18) { // length of the random string.
int index = (int) (rnd.nextFloat() * SALTCHARS.length());
salt.append(SALTCHARS.charAt(index));
}
String saltStr = salt.toString();
return saltStr;
}
String uni = getSaltString();
public String getUni(){
return this.uni;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_post);
toolbar = findViewById(R.id.postBar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Add New Post !");
postImage = findViewById(R.id.postImage);
postDesc = findViewById(R.id.inputDesc);
postButton = findViewById(R.id.post);
sr = FirebaseStorage.getInstance().getReference();
user = FirebaseAuth.getInstance().getCurrentUser();
String id = user.getUid();
dr = FirebaseDatabase.getInstance().getReference().child("Posts").child(uni);
dr.keepSynced(true);
postImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent gallery = new Intent();
gallery.setType("image/*");
gallery.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(gallery,"Select Image"),gallery_no);
}
});
postButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String desc = postDesc.getText().toString();
if(!TextUtils.isEmpty(desc) && postImageUri!=null ){
pd = new ProgressDialog(AddPost.this);
pd.setTitle("Uploading..");
pd.setMessage("Please Wait ..");
pd.setCanceledOnTouchOutside(false);
pd.show();
final String id = user.getUid();
File thumb = new File(postImageUri.getPath());
try {
compressor = new Compressor(AddPost.this)
.setMaxWidth(200)
.setMaxHeight(200)
.setQuality(75)
.compressToBitmap(thumb);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
compressor.compress(Bitmap.CompressFormat.JPEG, 100, baos);
final byte[] thumb_byte = baos.toByteArray();
final StorageReference path = sr.child("Post_Images").child(id + ".jpg");
final StorageReference thumb_nail = sr.child("Post_Images").child("Thumb_Nails").child(id+".jpg");
path.putFile(postImageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
#Override
public void onComplete(#NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
path.putFile(postImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
path.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
final String down_url = uri.toString();
UploadTask uploadTask = thumb_nail.putBytes(thumb_byte);
uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
#Override
public void onComplete(#NonNull Task<UploadTask.TaskSnapshot> task) {
thumb_nail.putFile(postImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
thumb_nail.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
final String thumb_down_url = uri.toString();
Map <String,Object> update_map = new HashMap<>();
update_map.put("image", down_url);
update_map.put("thumb_image", thumb_down_url);
update_map.put("desc",desc);
update_map.put("timestamp", date);
dr.updateChildren(update_map).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(AddPost.this, "Uploaded !", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(AddPost.this,MainActivity.class);
startActivity(intent);
finish();
pd.dismiss();
} else {
Toast.makeText(AddPost.this, "Error in uploading thumbnail!", Toast.LENGTH_SHORT).show();
pd.dismiss();
}
}
});
}
});
}
});
}
});
}
});
}
});
} else {
Toast.makeText(AddPost.this, "Error!", Toast.LENGTH_SHORT).show();
pd.dismiss();
}
}
});
}
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == gallery_no && resultCode == RESULT_OK) {
Uri imageUri = data.getData();
CropImage.activity(imageUri).setAspectRatio(1, 1).setMinCropWindowSize(500, 500)
.start(AddPost.this);
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
postImageUri = result.getUri();
postImage.setImageURI(postImageUri);
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
}
post_items.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/postCard"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.56"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/postImage"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:src="#drawable/default_avatar"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/postUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Username"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/postImage"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/postDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="Datee"
android:textStyle="italic"
app:layout_constraintStart_toEndOf="#+id/postImage"
app:layout_constraintTop_toBottomOf="#+id/postUsername" />
<ImageView
android:id="#+id/postBigImage"
android:layout_width="0dp"
android:layout_height="256dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/postImage"
tools:srcCompat="#tools:sample/avatars[3]" />
<TextView
android:id="#+id/postDesc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/postBigImage" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
To fix NullPointerException check null value of postItems.get(position) instead of postItems.get(position).getDesc() i.e.
#Override
public void onBindViewHolder(#NonNull final PostAdapter.ViewHolder holder, int position) {
if(postItems.get(position) != null) {
String descTe = postItems.get(position).getDesc();
holder.setDesc(descTe);
} else {
String def = "This is NULL";
holder.setDesc(def);
}
}
Also, you are initializing PostAdapter with blank List of postItems, i.e.
postItems = new ArrayList<>();
pa = new PostAdapter(postItems);
and Items are added later inSide onDataChange() method of the ValueEventListener. you must update your PostAdapter for the new updated List postItems and notify the adapter. like this.
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()){
//postItems.add(ds.getValue(PostActivity.class));
PostActivity pa =dataSnapshot.getValue(PostActivity.class);
postItems.add(pa);
}
pa.notifyDataSetChanged();
}
And for Cards not being displayed can you share the code of post_items.xml file.
(Posted an answer on behalf of the question author).
I solved every problem by removing whole PostAdapter class and then implementing FireebaseRecyclerAdapter in the HomeFragment itself ! My cards are displaying like charm now ! Thanks to everyone who replied to me here and helped me.

How to get data from cloud firestore and display it with recycler view

I have a cloud firestore database with a collection named Bus and three documents inside it as it is shown in the image
database picture
I have followed what is written in this link
How to display data from Firestore in a RecyclerView with Android?
three recycler views are being created and displayed but with empty data
also when I add a fourth document to Bus four recycler views are being created and displayed but with empty data
can you tell me how to fix my code?
model:
public class Bus {
public Bus(String driverName, int busNo, String contactNo) {
this.driverName = driverName;
this.busNo = busNo;
this.contactNo = contactNo;
}
public Bus(){ }
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
public int getBusNo() {
return busNo;
}
public void setBusNo(int busNo) {
this.busNo = busNo;
}
public String getContactNo() {
return contactNo;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
String driverName, contactNo;
int busNo ;
}
activity code :
public class ViewBusDetailes extends AppCompatActivity {
LinearLayoutManager linearLayoutManager;
FirestoreRecyclerAdapter<Bus, BusViewHolder> adapter;
class BusViewHolder extends RecyclerView.ViewHolder {
private View view;
BusViewHolder(View itemView) {
super(itemView);
view = itemView;
}
void setDetiles(String contactNoText , String driverNameText, int busNoText) {
TextView driverName;
TextView busNo;
TextView contactNo;
driverName = itemView.findViewById(R.id.driverNameDriverListView);
contactNo = itemView.findViewById(R.id.driverContactDriverListView);
busNo = itemView.findViewById(R.id.busNumber);
driverName.setText(driverNameText);
busNo.setText(String.valueOf(busNoText));
contactNo.setText(contactNoText);
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_bus_details__layout);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query query = rootRef.collection("Bus");
FirestoreRecyclerOptions<Bus> options = new FirestoreRecyclerOptions.Builder<Bus>()
.setQuery(query, Bus.class)
.build();
adapter = new FirestoreRecyclerAdapter<Bus, BusViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull BusViewHolder holder, int position, #NonNull Bus model) {
holder.setDetiles(model.getContactNo() , model.getDriverName() ,model.getBusNo());
}
#NonNull
#Override
public BusViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.bus_list_view, parent, false);
return new BusViewHolder(view);
}
};
recyclerView.setAdapter(adapter);
}
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
if (adapter != null) {
adapter.stopListening();
}
}
}
bus_list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="360dp"
android:layout_height="72dp"
android:background="?android:attr/selectableItemBackground"
android:orientation="horizontal"
android:padding="16dp">
<TextView
android:id="#+id/busNumber"
android:layout_width="40dp"
android:layout_height="40dp"
android:textSize="20sp"
android:gravity="center"
android:fontFamily="sans-serif"
android:textStyle="normal"
android:textColor="#ffffff"
android:background="#drawable/circle"
/>
<LinearLayout
android:layout_width="287dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/driverNameDriverListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:textSize="17sp"
android:fontFamily="sans-serif"
android:textStyle="normal"
android:textColor="#de000000"
android:lineSpacingExtra="8sp"
android:text="Anwar"
/>
<TextView
android:id="#+id/driverContactDriverListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_marginLeft="17dp"
android:fontFamily="sans-serif"
android:textStyle="normal"
android:textColor="#de000000"
android:lineSpacingExtra="6sp"
android:text="Dude can you solve this problem plea..."
/>
</LinearLayout>
</LinearLayout>
activity_view_bus_detiels.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.ViewBusDetailes"
android:id="#+id/container">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Do it like this
public class Bus {
private String driverName;
private int busNo;
private String contactNo;
public String getDriverName() {
return driverName;
}
public int getBusNo() {
return busNo;
}
public String getContactNo() {
return contactNo;
}
}
activity code :
public class ViewBusDetailes extends AppCompatActivity {
LinearLayoutManager linearLayoutManager;
FirestoreRecyclerAdapter adapter;
class BusViewHolder extends RecyclerView.ViewHolder {
TextView driverName;
TextView busNo;
TextView contactNo;
BusViewHolder(View itemView) {
super(itemView);
driverName = itemView.findViewById(R.id.driverNameDriverListView);
contactNo = itemView.findViewById(R.id.driverContactDriverListView);
busNo = itemView.findViewById(R.id.busNumber);
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_bus_details__layout);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query query = rootRef.collection("Bus");
FirestoreRecyclerOptions<Bus> options = new FirestoreRecyclerOptions.Builder<Bus>()
.setQuery(query, Bus.class)
.build();
adapter = new FirestoreRecyclerAdapter<Bus, BusViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull BusViewHolder holder, int position, #NonNull Bus model) {
holder.driverName.setText(model.getDriverName());
holder.busNo.setText(String.valueOf(model.getBusNo()));
holder.contactNo.setText(model.getContactNo());
}
#NonNull
#Override
public BusViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(ViewBusDetailes.this).inflate(R.layout.bus_list_view, parent, false);
return new BusViewHolder(view);
}
};
recyclerView.setAdapter(adapter);
}
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
if (adapter != null) {
adapter.stopListening();
}
}
}
Also, add this dependency if you haven't.
implementation 'com.firebaseui:firebase-ui-firestore:4.3.2'
Also, edit your Firebase Firestore rules from Firestore settings in the firebase console.
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
Check if it works.

Firestore recycler adapter not fetching document names

I'm trying fetch all documents using FirestoreRecyclerAdapter here if there are 7 documents the RecyclerView items successfully populates with 7 items but here problem is the items which are having a text view are not getting populated with document names. Please take a look at my source code:
FriendsResponse Class:
#IgnoreExtraProperties
public class FriendsResponse {
FirebaseFirestore db;
public String getTable1() {
return Table1;
}
public void setTable1(String table1) {
Table1 = table1;
}
private String Table1;
public FriendsResponse() {
}
public FriendsResponse(String Table1) {
this.Table1 = Table1;
}
}
TableList Fragment where recyclerview is initialized:
public class TableListFragment extends Fragment{
private FirebaseFirestore db;
private FirestoreRecyclerAdapter adapter;
String documentnm;
RecyclerView recyclerView;
FloatingActionButton addt;
private StaggeredGridLayoutManager _sGridLayoutManager;
public static TableListFragment newInstance() {
TableListFragment fragment = new TableListFragment();
return fragment;
}
public TableListFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tablelist, container, false);
recyclerView = view.findViewById(R.id.rectab);
addt=view.findViewById(R.id.addtab);
init();
getFriendList();
addt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return view;
}
private void init(){
_sGridLayoutManager = new StaggeredGridLayoutManager(3,
StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(_sGridLayoutManager);
db = FirebaseFirestore.getInstance();
}
private void getFriendList(){
Query query = db.collection("Order");
FirestoreRecyclerOptions<FriendsResponse> response = new FirestoreRecyclerOptions.Builder<FriendsResponse>()
.setQuery(query, FriendsResponse.class)
.build();
adapter = new FirestoreRecyclerAdapter<FriendsResponse, FriendsHolder>(response) {
#Override
public void onBindViewHolder(FriendsHolder holder, int position, FriendsResponse model) {
holder.exname.setText(model.getTable1());
holder.itemView.setOnClickListener(v -> {
Snackbar.make(recyclerView, model.getTable1(), Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
});
}
#Override
public FriendsHolder onCreateViewHolder(ViewGroup group, int i) {
View view = LayoutInflater.from(group.getContext())
.inflate(R.layout.list_item, group, false);
return new FriendsHolder(view);
}
#Override
public void onError(FirebaseFirestoreException e) {
Log.e("error", e.getMessage());
}
};
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
}
public class FriendsHolder extends RecyclerView.ViewHolder {
TextView exname;
public FriendsHolder(View itemView) {
super(itemView);
exname= itemView.findViewById(R.id.topicname);
}
}
#Override
public void onStart() {
super.onStart();
adapter.startListening();
}
#Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
#Override
public void onDetach() {
super.onDetach();
}
}
This is the code of list_item:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView android:id="#+id/cardvw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="6dp"
card_view:cardElevation="3dp"
card_view:cardUseCompatPadding="true"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto">
<LinearLayout android:orientation="vertical" android:padding="5dp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/topiclogo"
android:layout_width="match_parent"
android:layout_gravity="center"
android:src="#drawable/table"
android:layout_height="wrap_content"
/>
<TextView android:textSize="15sp"
android:textStyle="bold"
android:textAlignment="center"
android:textColor="#ffffa200"
android:id="#+id/topicname"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
As I understand, you want to set the id of the document to that TextView. So because those names are actually documents ids, you should use the following lines of code inside onBindViewHolder() method:
String id = getSnapshots().getSnapshot(position).getId();
holder.exname.setText(id);
The POJO class that you are using is useful when getting the properties of the documents, not to get the document ids.

Categories