Images showing slow from firebase realtime database - android

I am creating an Android app which is based on wallpaper. I have different categories in my app that shows images and after clicking on particular image it will show full image. The problem is when I click on any categories the images are shown are very slow. I am retrieving images from firebase realtime database. I am showing images in grid layout in two columns. I have also compress all the images then to the process of showing images is very slow.
This is one of the category code.
public class AnimalWallpaper extends AppCompatActivity {
private RecyclerView recyclerView;
private ProgressBar progressBar;
private DatabaseReference reference;
private ArrayList<String> list;
private WallpaperAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animal_wallpaper);
reference = FirebaseDatabase.getInstance().getReference().child("animal");
recyclerView = findViewById(R.id.recyclerViewAnimal);
progressBar = findViewById(R.id.progressBarAnimal);
getData();
}
private void getData() {
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull #NotNull DataSnapshot snapshot) {
progressBar.setVisibility(View.GONE);
list = new ArrayList<>();
for (DataSnapshot shot : snapshot.getChildren()) {
String data = shot.getValue().toString();
list.add(data);
}
recyclerView.setLayoutManager(new GridLayoutManager(AnimalWallpaper.this, 2));
adapter = new WallpaperAdapter(list, AnimalWallpaper.this);
recyclerView.setAdapter(adapter);
progressBar.setVisibility(View.GONE);
}
#Override
public void onCancelled(#NonNull #NotNull DatabaseError error) {
progressBar.setVisibility(View.GONE);
Toast.makeText(AnimalWallpaper.this, "Error : "+error.getMessage() , Toast.LENGTH_SHORT).show();
}
});
}
}
This is wallpaperadapter class code.
public class WallpaperAdapter extends RecyclerView.Adapter<WallpaperAdapter.WallpaperViewHolder> {
private ArrayList<String> list;
private Context context;
public WallpaperAdapter(ArrayList<String> list, Context context) {
this.list = list;
this.context = context;
}
#NonNull
#org.jetbrains.annotations.NotNull
#Override
public WallpaperViewHolder onCreateViewHolder(#NonNull #org.jetbrains.annotations.NotNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.custom_image_layout, parent, false );
return new WallpaperViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull #org.jetbrains.annotations.NotNull WallpaperAdapter.WallpaperViewHolder holder, int position) {
Glide.with(context).load(list.get(position)).into(holder.imageView);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, FullImageActivity.class);
intent.putExtra("images", list.get(position));
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return list.size();
}
public class WallpaperViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
public WallpaperViewHolder(#NonNull #NotNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.item_image);
}
}
}

Related

get data from specific item in recyclerview according to position passed from firebase

I have created a simple notes app in which I'm storing data in firebase. Showing the list of notes(time and few words of a note) in recyclerview. Now I want when I click in a particular note, it take to different activity where the details of the note will be shown. I am not able to get data from firebase column wise(note and time).
CustomAdapter
Here I'm sending the position of the item clicked to NotesDetails class
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
Context context;
ArrayList<Model> models;
public CustomAdapter(Context context, ArrayList<Model> models){
this.context = context;
this.models = models;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.list_content, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.time_text.setText(models.get(holder.getBindingAdapterPosition()).getText());
holder.string_text.setText(models.get(holder.getBindingAdapterPosition()).getNotes());
holder.constraintLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context.getApplicationContext(), NotesDetails.class);
intent.putExtra("position", String.valueOf(holder.getBindingAdapterPosition()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() { return models.size(); }
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView string_text, time_text;
ConstraintLayout constraintLayout;
public MyViewHolder(View itemView) {
super(itemView);
string_text = itemView.findViewById(R.id.textView);
time_text = itemView.findViewById(R.id.timeshow);
constraintLayout = itemView.findViewById(R.id.constraintLayout);
}
}
}
NotesDetails
I want to show time and details of particular item here in testShow and timeShow
public class NotesDetails extends AppCompatActivity {
ArrayList<Model> models;
TextView textShow, timeShow;
DBHelper myDB;
DatabaseReference dbReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notes_details);
textShow = findViewById(R.id.notesShow);
timeShow = findViewById(R.id.timeshow);
myDB = new DBHelper(this);
Intent intent = getIntent();
int position = Integer.parseInt(intent.getStringExtra("position"));
dbReference = FirebaseDatabase.getInstance().getReference("Model").child("notes");
dbReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull #NotNull DataSnapshot snapshot) {
for(DataSnapshot dataSnapshot: snapshot.getChildren()){
Model m1 = dataSnapshot.getValue(Model.class);
textShow.setText((CharSequence) m1);
}
}
#Override
public void onCancelled(#NonNull #NotNull DatabaseError error) {
}
});
}
}

RecycerView always reset back to first position after inserting new Firebase data

My recyclerview always reset to first position after new data is inserted to firebase. In my TestAdapter class, I have an onClickListener which increments like on a post when clicked. Whenever I click on this "like" button, view is reset to first position. I believe this issue is related to setAdapter() being called on data changed. Please help!
MainActivity fetches "post" data from Firebase and populates it in TestAdapter class.
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private TestAdapter testAdapter;
private ArrayList<Post> postList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
fetchPostFromDB();
}
private void fetchPostFromDB() {
DatabaseReference databaseRef = FirebaseDatabase.getInstance().getReference("Post");
databaseRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
postList = new ArrayList<>();
for (DataSnapshot snap : snapshot.getChildren()) {
String post = snap.child("post").getValue().toString();
Post post = new Post(post);
postList.add(post);
}
testAdapter = new TestAdapter(getApplicationContext(), postList);
recyclerView.setAdapter(testAdapter);
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
TestAdapter binds the data (postList) and has an onClickListener.
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.TestHolder> {
private List<Post> postList;
private Context context;
private DatabaseReference databaseReference;
public TestAdapter(Context ct, ArrayList<Post> postList) {
context = ct;
this.postList = postList;
}
#NonNull
#Override
public TestHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.post_item, parent, false);
final TestHolder holder = new TestHolder(view);
holder.ibLike.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String postID = postList.get(holder.getAdapterPosition()).getPostID();
databaseReference = FirebaseDatabase.getInstance().getReference("Posts").child(postID).child("likes");
int incrementLike = postList.get(holder.getAdapterPosition()).getLikes() + 1;
databaseReference.setValue(incrementLike);
}
});
return holder;
}
#Override
public void onBindViewHolder(#NonNull final TestHolder holder, final int position) {
holder.tvPost.setText(postList.get(position).getPost());
holder.tvVotes.setText(Integer.toString(postList.get(position).getLikes()));
}
#Override
public int getItemCount() {
return postList.size();
}
public static class TestHolder extends RecyclerView.ViewHolder {
TextView tvPost, tvLikes;
ImageButton ibLike;
public TestHolder(#NonNull View itemView) {
super(itemView);
tvPost = itemView.findViewById(R.id.tvPost);
tvLikes = itemView.findViewById(R.id.tvLikes);
ibLike = itemView.findViewById(R.id.ibLike);
}
}
Here need to initialize your adapter class from main activity. and then when data received just call adapter.notifiyDatasetChanged like:
onCreate():
postList = new ArrayList<>();
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
testAdapter = new TestAdapter(getApplicationContext(), postList);
recyclerView.setAdapter(testAdapter);
and from your fetchPostFromDB delete adapter initialization insted of this just put this line testAdapter.notifiyDataSetChanged()
And when click on text button you can update/handle like this:
holder.ibLike.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String postID = postList.get(holder.getAdapterPosition()).getPostID();
databaseReference = FirebaseDatabase.getInstance().getReference("Posts").child(postID).child("likes");
int incrementLike = postList.get(holder.getAdapterPosition()).getLikes() + 1;
postList.get(holder.getAdapterPosition()).setLikes(incrementLike);
databaseReference.setValue(incrementLike);
notifyDataSetChanged();
}
});

why png files are not loading into recyclerview

I am currently working at a Android project, where I have to upload png files into Firebase Realtime-database. My app is retrieving images but when I used png files it's loading only five png files, I don't know what happened to the other png files.
When I make an edit with the image url one of the another image turn not working and the one I edited started to work.
I am using Picasso library to view the images...
Does anybody else encountered the same error...
I should complete the project as soon as possible
Should this problem solves if I use glide instead of picasso???
myadapter.java
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private DatabaseReference reference;
static Context context;
static ArrayList<Profile> profiles;
public MyAdapter(Context c, ArrayList<Profile> p) {
context = c;
profiles = p;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.recyclerview_item, parent, false));
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.title.setText(profiles.get(position).getTitle());
holder.desc.setText(profiles.get(position).getDesc());
Picasso.get().load(profiles.get(position).getImage()).into(holder.image);
}
#Override
public int getItemCount() {
return profiles.size();
}
static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView title, desc;
ImageView image;
Button btn;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
desc = (TextView) itemView.findViewById(R.id.desc);
image = (ImageView) itemView.findViewById(R.id.image);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Intent intent = new Intent(context, Main2Activity.class);
intent.putExtra("URL", profiles.get(getAdapterPosition()).getImage());
context.startActivity(intent);
}
}
}
mainactivity.java
public class helmet extends AppCompatActivity {
DatabaseReference reference;
RecyclerView recyclerView;
ArrayList<Profile> list;
public MyAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_helmet);
recyclerView = (RecyclerView) findViewById(R.id.recyclle);
int numberOfColumns = 3;
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL));
recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns));
reference = FirebaseDatabase.getInstance().getReference().child("helmet");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
list = new ArrayList<Profile>();
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
Profile p = dataSnapshot1.getValue(Profile.class);
list.add(p);
}
adapter = new MyAdapter(helmet.this, list);
recyclerView.setAdapter(adapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(helmet.this, "Opsss.... Something is wrong", Toast.LENGTH_SHORT).show();
}
});
}
....

firebase recycler adapter doesnt load images but show data

I am beginner in android studio I am trying to load data from firebase Database into recyclerview but I have a problem in below code that RecyclerView doesn't load images but show data..and android studio doesn't show any error and I also search in google but I did not find any solution..
Now below is my code...Thanku in andvance
Adapter class
private Context mContext;
private List<Upload> mUpload;
public ImageAdapter(Context context, List<Upload> uploads) {
mContext = context;
mUpload = uploads;
}
//this method will create views
#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);
}
//this method is used for databinding that binds our data with views
#Override
public void onBindViewHolder(#NonNull ImageViewHolder holder, int position) {
Upload uploadCurrent=mUpload.get(position);
holder.textViewName.setText(uploadCurrent.getmName());
Picasso.with(mContext)
.load(uploadCurrent.getmImageUrl())
.fit()
.centerCrop()
.into(holder.imageView);
}
#Override
public int getItemCount() {
return mUpload.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 calss
public class Upload {
private String mName;
private String mImageUrl;
public Upload () {}
public Upload(String name, String imageUrl) {
if(name.trim().equals(""))
{
name="No name";
}
this.mName = name;
this.mImageUrl = imageUrl;
}
public String getmName() {
return mName;
}
public void setmName(String mName) {
this.mName = mName;
}
public String getmImageUrl() {
return mImageUrl;
}
public void setmImageUrl(String mImageUrl) {
this.mImageUrl = mImageUrl;
}
}
This is MainAvtivity/ImageActivity
public class ImagesActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private ImageAdapter mAdapter;
private DatabaseReference mDatabaseRef;
private List<Upload> mUploads;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_images);
mRecyclerView=findViewById(R.id.recyler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
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);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(ImagesActivity.this,databaseError.getMessage(),Toast.LENGTH_SHORT).show();
}
});
}
}
I am unable to comment but did you check that List which is inside Adapter has Photo URL?
change
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);
}
into
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot: dataSnapshot.getChildren())
{
Upload upload=postSnapshot.getValue(Upload.class);
mAdapter.list.add(upload);
mAdapter.notifyDataSetChanged();
}
}
Adapter class
private Context mContext;
ArrayList<Upload> list= new ArrayList<>();
public mAdapter(Context context ) {
mContext = context;
}
MAinActivity
mAdapter=new ImageAdapter(ImagesActivity.this);
mRecyclerView.setAdapter(mAdapter);
The logic behind this is that Adapterclass has a Arraylist. When you obtain data from background Thread on onDataChange. You add it to list directly and notify adapter that dataset has changed

Firebase list of data is unable to load in Custom recycler adaper

I am trying to load firebase list of data in my custom recycler adapter but I am unable to load it.
this code is displaying data in my logcat but I am unable to load in my app.
logcat showing no errors though. If I use firebase UI and try to do this thing then it is working fine but I want to inset native ads in recycler view to I had to migrate to custom adapter and now I am stuck with this issue. :(
my custom recycler adapet.
public class AuthorNameCustomRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
ArrayList<AuthorsCLA> authorlist;
Context mcontext;
public class NamesHolder extends RecyclerView.ViewHolder {
public TextView name;
public NamesHolder(View itemView) {
super(itemView);
name = (TextView)itemView.findViewById(R.id.authorNameTextView);
}
}
public AuthorNameCustomRecyclerAdapter(ArrayList<AuthorsCLA> authorlist, Context mcontext) {
this.authorlist = authorlist;
this.mcontext = mcontext;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType) {
case 1: {
View view = inflater.inflate(R.layout.author_list_cv, parent, false);
viewHolder = new NamesHolder(view);
break;
}
}
return viewHolder;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final AuthorsCLA authorsModel = authorlist.get(holder.getAdapterPosition());
switch (holder.getItemViewType()){
case 1:{
NamesHolder nameHolder = (NamesHolder) holder;
nameHolder.name.setText(authorsModel.getAuthorname());
nameHolder.name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), AuthorQuotesDetailActvity.class);
mcontext.startActivity(intent);
}
});
}
}
}
#Override
public int getItemCount() {
return authorlist.size();
}
}
here is the class where i am calling custom recycler adapter.
public class AuthorsList extends AppCompatActivity {
private RecyclerView recyclerViewAuthor;
private StaggeredGridLayoutManager mlayoutManager;
private GridLayoutManager manager;
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference mdatabaseReference = database.getReference();
private AdView mAdView;
private RecyclerView.Adapter adapter;
private ArrayList<AuthorsCLA> authorlist;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.authors_list);
MobileAds.initialize(this, "ca-app-pub-7854400750152181~4721849113");
mAdView = (AdView) findViewById(R.id.adView1);
AdRequest adRequest = new AdRequest.Builder().addTestDevice("2A79E20CADE70C60CF36B2E8EE9103FF").build();
mAdView.loadAd(adRequest);
recyclerViewAuthor = (RecyclerView) findViewById(R.id.authorsRecyclerView);
DatabaseReference mListItemRef = mdatabaseReference.child("en/of");
authorlist = new ArrayList<>();
manager = new GridLayoutManager(this,2);
recyclerViewAuthor.setLayoutManager(manager);
ChildEventListener childEventListener = new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.d("Added",dataSnapshot.getKey());
fetchData(dataSnapshot);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
};
mListItemRef.addChildEventListener(childEventListener);
adapter = new AuthorNameCustomRecyclerAdapter(authorlist, this);
recyclerViewAuthor.setAdapter(adapter);
}
private void fetchData(DataSnapshot dataSnapshot) {
AuthorsCLA listItem=dataSnapshot.getValue(AuthorsCLA.class);
authorlist.add(listItem);
// recyclerViewAuthor.setAdapter(adapter);
}
}
onChildAdded() will be called each time when the child is fetched by firebase call. This is asynchronous process.
In your code you are adding it into ArrayList<>, but somehow you are not notifying the adapter that child is being added. It is safe to do that in your
private void fetchData(DataSnapshot dataSnapshot) {
AuthorsCLA listItem = dataSnapshot.getValue(AuthorsCLA.class);
authorlist.add(listItem);
adapter.notifyItemInserted(authorlist.size() - 1); // Notify the adapter
}

Categories

Resources