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();
}
});
}
....
Related
I'm loading some data into a RecyclerView from Firebase Realtime Database. This RecyclerView is in a fragment and I want to open a new activity when an item is clicked. But when i add the onClickListener to an item from the adapter, my click doesn't even recognized by the app. I'm not sure what I'm doing wrong here because this method worked fine for a RecyclerView when it is inside of a normal activity.
This is a project for my university and I should submit it tomorrow. So I can really use some help.
This is the my data Adapter class,
public class Adapter_NoLimit extends RecyclerView.Adapter<Adapter_NoLimit.LViewHolder> {
Context context;
ArrayList<Helper> list_nl;
public Adapter_NoLimit(Context context, ArrayList<Helper> list_nl) {
this.context = context;
this.list_nl = list_nl;
}
#NonNull
#Override
public Adapter_NoLimit.LViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.itemlist, parent, false);
return new LViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull Adapter_NoLimit.LViewHolder holder, int position) {
//final Helper temp = list_nl.get(position);
String fname_txt = list_nl.get(position).getFname();
String catg_txt = list_nl.get(position).getSpin();
String prof_img = list_nl.get(position).getProfile_url();
holder.setUsers(fname_txt, catg_txt, prof_img);
holder.f_name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AppCompatActivity activity = (AppCompatActivity) v.getContext();
Intent i = new Intent(activity, UserProfile_VV.class);
i.putExtra("full name", fname_txt);
activity.startActivity(i);
}
});
}
#Override
public int getItemCount() {
return list_nl.size();
}
public class LViewHolder extends RecyclerView.ViewHolder{
private TextView f_name, catG;
private ImageView profile_pic;
public LViewHolder(#NonNull View itemView) {
super(itemView);
f_name = itemView.findViewById(R.id.username_view);
catG = itemView.findViewById(R.id.category_view);
profile_pic = itemView.findViewById(R.id.userimage);
}
public void setUsers(String fname_txt, String catg_txt, String prof_img) {
f_name.setText(fname_txt);
catG.setText(catg_txt);
Picasso.get().load(prof_img).fit().into(profile_pic);
}
}
}
This is one of fragments,
public class Beauty_Frag extends Fragment {
RecyclerView recyclerView;
DatabaseReference reference, user_ref;
ArrayList<Helper> list;
Adapter adapter;
FirebaseAuth auth;
String uid;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_beauty, container, false);
recyclerView = v.findViewById(R.id.beauty_recycler_frag);
reference = FirebaseDatabase.getInstance().getReference();
user_ref = reference.child("users");
auth = FirebaseAuth.getInstance();
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
//searchField = findViewById(R.id.search_field);
list = new ArrayList<>();
adapter = new Adapter(getContext(), list);
recyclerView.setAdapter(adapter);
user_ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull /*#org.jetbrains.annotations.NotNull*/ DataSnapshot snapshot) {
for (DataSnapshot key : snapshot.getChildren()) {
if( key.child("spin").getValue().toString().equals("Beauty")) {
Helper helper = key.getValue(Helper.class);
list.add(helper);
adapter.notifyDataSetChanged();
}
}
}
#Override
public void onCancelled(#NonNull /*#org.jetbrains.annotations.NotNull*/ DatabaseError error) {
}
});
return v;
}
}
You are very welcome if you have any solution.
Use below code for onClicklistner:
holder.f_name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(context, UserProfile_VV.class);
i.putExtra("full name", fname_txt);
context.startActivity(i);
}
});
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);
}
}
}
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) {
}
});
}
}
I have two Models (ModelChat and ModelUsers) and their class adapters (PostsAdapter and AllUsersAdapter).
I want to get and set user info such as name and email from the ModelUser and get and set post information from the ModelPost from Firebase database into the same recyclerview in my HomeActivity.
The codes are as shown below:
PostsAdapter.java
public class PostsAdapter extends RecyclerView.Adapter<PostsAdapter.MyHolder>{
Context context;
List<ModelPost> postList;
public PostsAdapter(Context context, List<ModelPost> postList) {
this.context = context;
this.postList = postList;
}
#NonNull
#Override
public MyHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//inflate layout row_post.xml
View view = LayoutInflater.from(context).inflate(R.layout.row_posts, parent, false);
return new MyHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyHolder holder, int position) {
//get data
String userUID = postList.get(position).getUserUID();
String userEmail = postList.get(position).getUserEmail();
String userProfilePic = postList.get(position).getUserProfilePic();
String firstName = postList.get(position).getUserFirstName();
String lastName = postList.get(position).getUserLastName();
String userNickName = postList.get(position).getUserNickName();
String postTimeStamp = postList.get(position).getPostTime();
String userPost = postList.get(position).getUserPost();
//set data
//set data
Glide
.with(context)
.load(userProfilePic)
.apply(RequestOptions.circleCropTransform())
.into(holder.avatarTv);
holder.firstnameTv.setText(firstName);
holder.lastnameTv.setText(lastName);
holder.nicknameTv.setText(userNickName);
}
#Override
public int getItemCount() {
return postList.size();
}
//view holder class
class MyHolder extends RecyclerView.ViewHolder {
//views from row_posts layout
public MyHolder(#NonNull View itemView) {
super(itemView);
//init view
//my views here
}
}
}
AllUsersAdapter.java
public class AllUsersAdapter extends RecyclerView.Adapter<AllUsersAdapter.MyHolder>{
Context context;
List<ModelUser> userList;
//Constructor > Generate
public AllUsersAdapter(Context context, List<ModelUser> userList) {
this.context = context;
this.userList = userList;
}
#NonNull
#Override
public MyHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//Inflate layout (row_user)
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_all_users, parent, false);
return new MyHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyHolder holder, int position) {
//get data
String userUID = userList.get(position).getUid();
String userProfilePic = userList.get(position).getProfilepic();
String userName = userList.get(position).getFirstname() + " " + userList.get(position).getLastname();
String userNickname = userList.get(position).getNickname();
String userStatus = userList.get(position).getStatus();
//set data
Glide
.with(context)
.load(userProfilePic)
.apply(RequestOptions.circleCropTransform())
.into(holder.mAvatarIv);
holder.mNameTv.setText(userName);
holder.mNicknameTv.setText(userNickname);
holder.mStatusTv.setText(userStatus);
//Handle item click
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/*Click user from user list to start conversation
*Start Activity by putting UID of receiver
*we will use that UID to identify the user we are gonna chat */
Intent intent = new Intent(context, MessageActivity.class);
intent.putExtra("userUid", userUID);
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return userList.size();
}
class MyHolder extends RecyclerView.ViewHolder {
public MyHolder(#NonNull View itemView) {
super(itemView);
//init my views here
}
}
}
HomeActivity.java
public class HomeActivity extends AppCompatActivity {
FirebaseAuth firebaseAuth;
RecyclerView recyclerView;
List<ModelPost> postList;
PostsAdapter postsAdapter;
String mUID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
firebaseAuth = FirebaseAuth.getInstance();
//recyclerview and its properties
recyclerView = findViewById(R.id.posts_recyclerview);
//init post list
postList = new ArrayList<>();
loadPosts();
}
private void loadPosts() {
//path of all posts
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");
//get all data from this reference
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
postList.clear();
for (DataSnapshot ds: snapshot.getChildren()) {
ModelPost modelPost = ds.getValue(ModelPost.class);
postList.add(modelPost);
//adapter
postsAdapter = new PostsAdapter(HomeActivity.this, postList);
//set adapter to recyclerview
recyclerView.setAdapter(postsAdapter);
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
//in case of errors
//Toast.makeText(HomeActivity.this, ""+error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
Try using ConcatAdapter.
An RecyclerView.Adapter implementation that presents the contents of multiple adapters in sequence. More info
Concatenate adapters sequentially with ConcatAdapter:
https://medium.com/androiddevelopers/merge-adapters-sequentially-with-mergeadapter-294d2942127a
I think this will help
MyAdapter adapter1 = ...;
AnotherAdapter adapter2 = ...;
ConcatAdapter concatenated = new ConcatAdapter(adapter1, adapter2);
recyclerView.setAdapter(concatenated);
I am developing an app which retrieves images from Firebase. I have retrieved all the images from Firebase successfully but when I select some image as a favorite, it duplicate all the selected images twice or thrice in the new activity in RecyclerView. Please help me anyone. Thanks in advance..
Here is my code.
public class Fragment3 extends Fragment {
ArrayList<String> ImagesList = (ArrayList) (ListofData.FavoriteList = new ArrayList<>());
Set<String> hashset = new HashSet<String>(ImagesList);
RecyclerView recyclerView;
FragThreeAdapter adapter;
public Fragment3() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fragment3, container, false);
FirebaseHandler firebaseHandler = new FirebaseHandler(getActivity());
firebaseHandler.getFavoriteUrlRef().addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot d : dataSnapshot.getChildren()) {
String images = d.getValue().toString();
ImagesList.add(images);
adapter.notifyDataSetChanged();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
ArrayList<String> Images = new ArrayList<>(hashset);
recyclerView = view.findViewById(R.id.frag3_fav_recyclerView);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
adapter = new FragThreeAdapter(Images, getActivity());
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
return view;
}
}
My Click Listener
favoriteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(activity, "saved to favorite", Toast.LENGTH_SHORT).show();
reference = FirebaseDatabase.getInstance().getReference().child("Favorite");
String key = reference.push().getKey();
reference.child(key).setValue(imagePaths.get(position));
My Adapter Class
public class FragThreeAdapter extends RecyclerView.Adapter<FragThreeAdapter.myHolder> {
private List<String> list;
private Context context;
public FragThreeAdapter(List<String> list, Context context) {
this.list = list;
this.context = context;
}
#NonNull
#Override
public myHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater= LayoutInflater.from(context);
View view= inflater.inflate(R.layout.list_item,parent,false);
return new myHolder(view);
}
#Override
public void onBindViewHolder(#NonNull myHolder holder, int position) {
Glide.with(context)
.load(list.get(position))
.into(holder.imageView);
holder.imageView.setOnClickListener(new OnImageClickListner(position));
}
#Override
public int getItemCount() {
return list.size();
}
public class myHolder extends RecyclerView.ViewHolder{
ImageView imageView;
public myHolder(View itemView) {
super(itemView);
imageView= itemView.findViewById(R.id.imageView_id);
}
}
class OnImageClickListner implements View.OnClickListener{
int position;
public OnImageClickListner(int position){
this.position= position;
}
#Override
public void onClick(View v) {
Intent intent= new Intent(context,Frag3_Favrit_FullActvity.class);
intent.putExtra("array_position",position);
intent.putStringArrayListExtra("list", (ArrayList<String>) list);
context.startActivity(intent);
}
}
}
You can try to make like this:
public void onBindViewHolder(#NonNull myHolder holder, int position) {
if (list.get(position) != null) {
Glide.with(context)
.load(list.get(position))
.into(holder.imageView);
} esle {
holder.imageView.setImageResource(R.mipmap.some_default_cover);
}
holder.imageView.setOnClickListener(new OnImageClickListner(position));
}