I'm writing a small group chat application in Android Studio and using Firebase as the backend. I'm trying to allow the users to post images however everytime I send an image to firebase storage and retrieve it in order to post into a recycler view using Glide, it duplicates in the recycler view. I've confirmed the image in the firebase storage is NOT duplicating, it's just on the recycler view on the app itself. I've been struggling with this issue for days and cannot for the life of me find why it's duplicating.
Here's my code:
Uploads and Retrieves the image URL in Firebase
public void uploadImage(final Uri filePath) {
if (filePath != null) {
final String pathToImage = "chatImages/" + user.getUid()+"/"+ UUID.randomUUID();
StorageReference storeRef = storageReference.child(pathToImage);
UploadTask uploadTask = storeRef.putFile(filePath);
uploadTask.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.e(TAG, "Upload Failed");
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Log.e(TAG, "Upload Successful");
//now that the image has been uploaded, send to chat
storageReference.child(pathToImage).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
//adds photo, username and message to the arraylists\
imagePath = uri.toString();
Log.e(TAG, "done");
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.e(TAG, "Failed to Retrieve Image Message");
}
});
}
});
}
}
Adds the image and other post info to the recycler view
db.collection("chats").document("0fv4XvLpi8dWivyohQbk").collection("chatMessages")
.orderBy("TimeStamp")
.addSnapshotListener(new EventListener<QuerySnapshot>() {
#Override
public void onEvent(#Nullable QuerySnapshot queryDocumentSnapshots, #Nullable FirebaseFirestoreException e) {
if (e != null) {
Log.e(TAG, "Listen Failed", e);
return;
}
//temporay arraylists
final ArrayList<String> uIdAL = new ArrayList<>();
//gets any updates
for (DocumentChange newdoc : queryDocumentSnapshots.getDocumentChanges()) {
if (newdoc.getType() == DocumentChange.Type.ADDED) {
Log.e(TAG, "new message" + newdoc.getDocument().getData());
//saves userIDs for later use in getting profile pics
uIdAL.add((String) newdoc.getDocument().getData().get("userID"));
//temporary profile picture
if(chatPhotoUrl != null && ((String) newdoc.getDocument().getData().get("userID")).equals(user.getUid()))
{
messageAvatars.add(chatPhotoUrl.toString());
}
else
{
messageAvatars.add("https://pictures.digitalrev.com/image/upload/v1454493563/gzev2fhfpoizg3bpozjr.jpg");
}
//get usernames and messages
userNames.add((String) newdoc.getDocument().getData().get("userName"));
messages.add((String) newdoc.getDocument().getData().get("message"));
imageMessages.add((String) newdoc.getDocument().getData().get("imageMessage"));
//add to recycler view
adapter.notifyItemInserted(userNames.size());
rv.scrollToPosition(userNames.size() - 1);
Log.e(TAG, "here");
}
}
}
});
}
Recycler View Adapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
private static final String TAG = "RecyclerViewAdapter";
private ArrayList<String> messageAvatars = new ArrayList<>();
private ArrayList<String> userNames = new ArrayList<>();
private ArrayList<String> messages = new ArrayList<>();
private ArrayList<String> imageMessages = new ArrayList<>();
private Context context;
public RecyclerViewAdapter(ArrayList<String> messageAvatars, ArrayList<String> userNames, ArrayList<String> messages, ArrayList<String> imageMessages, Context context) {
this.messageAvatars = messageAvatars;
this.userNames = userNames;
this.messages = messages;
this.imageMessages = imageMessages;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerviewitem,parent, false);
ViewHolder viewholder = new ViewHolder(view);
return viewholder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
Log.e(TAG, "onBindViewHolder: called");
//sets the fields in the recyclerview item
Glide.with(context).asBitmap().load(messageAvatars.get(position)).into(holder.messageImage);
holder.userName.setText(userNames.get(position));
holder.messageText.setText(messages.get(position));
Glide.with(context).asBitmap().load(imageMessages.get(position)).into(holder.imageMessage);
//triggers if the recyclerview item is clicked
holder.parentLayout.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view)
{
Log.e(TAG, "onClick: clicked");
}
});
}
#Override
public int getItemCount() {
return userNames.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
public class ViewHolder extends RecyclerView.ViewHolder{
CircleImageView messageImage;
TextView userName;
TextView messageText;
ImageView imageMessage;
ConstraintLayout parentLayout;
public ViewHolder(View itemView) {
super(itemView);
messageImage = itemView.findViewById(R.id.chatImageView);
userName = itemView.findViewById(R.id.usernametextView);
messageText = itemView.findViewById(R.id.messagetextView);
imageMessage = itemView.findViewById(R.id.imageView);
parentLayout = itemView.findViewById(R.id.parentlayout);
}
}
Any and all help would be appreciated!
Related
I have a recycler view setup for my chat app. I would like to add it so users are able to send pictures in the chat. I added an image view and added the code to upload the image to firestore. Where I am confused is implementing the code to display it in the recycler view.
Model:
package com.example.android.debateapp.Message;
public class Message {
private String ChatMessage;
private String mChatImageURL;
public Message(){
//Empty constructor needed
}
public Message(String ChatMessage, String mChatImageURL ){
this.ChatMessage = ChatMessage;
this.mChatImageURL = mChatImageURL;
}
public String getChatMessage() {
return ChatMessage;
}
public String getmChatImageURL() {
return mChatImageURL;
}
}
MessageRecyclerAdapter:
public class MessageRecyclerAdapter extends FirestoreRecyclerAdapter<Message, MessageRecyclerAdapter.MessageHolder> {
public MessageRecyclerAdapter(#NonNull FirestoreRecyclerOptions<Message> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull MessageHolder holder, int position, #NonNull Message model) {
holder.ChatMessage.setText(model.getChatMessage());
}
#NonNull
#Override
public MessageHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.message_item, viewGroup, false);
return new MessageHolder(v);
}
class MessageHolder extends RecyclerView.ViewHolder{
TextView ChatMessage;
CircleImageView MessageProfilePicture;
ImageView ChatMessageImage;
public MessageHolder(#NonNull View itemView) {
super(itemView);
ChatMessage = itemView.findViewById(R.id.Message_Chat_Content);
MessageProfilePicture = itemView.findViewById(R.id.Chat_Profile_picture);
ChatMessageImage = itemView.findViewById(R.id.chat_Message_image);
}
public void setChatImage(final String downloadUri) {
ChatMessageImage = itemView.findViewById(R.id.chat_Message_image);
Glide.with(itemView.getContext()).load(downloadUri).into(ChatMessageImage);
}
}
}
ChatActivity
public class ChatActivity extends AppCompatActivity {
public static final int DEFAULT_MSG_LENGTH_LIMIT = 1000;
private static final int GALLERY_PICK = 1;
private ListView mMessageListView;
private ImageButton mPhotoPickerButton;
private EditText mMessageEditText;
private Button mSendButton;
private String mUsername;
private ChildEventListener mChildEventListner;
private ValueEventListener mValueEventListner;
private FirebaseUser mCurrentUser;
private StorageReference storageReference;
private StorageReference mChatPhotosStorageReference;
private Context mContext;
private MessageRecyclerAdapter adapter;
private ImageView chatimage;
private String TESTmessagedoc;
private ImageView chatProfile;
//FIRE STORE
private DocumentReference mMessageDoc;
private FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
private CollectionReference collectionReference = (firebaseFirestore).collection("Messages");
private CollectionReference colRef;
private static final String TAG = ChatActivity.class.getName();
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_activity);
storageReference = FirebaseStorage.getInstance().getReference();
chatProfile = findViewById(R.id.Chat_Profile_picture);
//chatimage = findViewById(R.id.photoImageView);
final String messageDoc = getIntent().getStringExtra("Message_ID");
TESTmessagedoc = messageDoc;
mMessageDoc = firebaseFirestore.collection("Messages").document(messageDoc);
colRef = firebaseFirestore.collection("Messages").document(messageDoc).collection("chats");
mPhotoPickerButton = (ImageButton) findViewById(R.id.photoPickerButton);
mMessageEditText = (EditText) findViewById(R.id.messageEditText);
mSendButton = (Button) findViewById(R.id.sendButton);
mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
final String current_uid = mCurrentUser.getUid();
// ImagePickerButton shows an image picker to upload a image for a message
mPhotoPickerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(galleryIntent, "Select Image"), GALLERY_PICK);
}
});
setUpRecyclerView();
// Enable Send button when there's text to send
mMessageEditText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.toString().trim().length() > 0) {
mSendButton.setEnabled(true);
} else {
mSendButton.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable editable) {
}
});
mMessageEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(DEFAULT_MSG_LENGTH_LIMIT)});
mSendButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String MessageText = mMessageEditText.getText().toString();
Map<String, Object> chatMap = new HashMap<>();
chatMap.put("ChatMessage", MessageText);
chatMap.put("User ID", current_uid);
chatMap.put("Timestamp", FieldValue.serverTimestamp());
firebaseFirestore.collection("Messages")
.document(messageDoc).collection("chats")
.add(chatMap)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
#Override
public void onSuccess(DocumentReference documentReference) {
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
}
});
// Clear input box
mMessageEditText.setText("");
}
});
}
private void setUpRecyclerView() {
Query query = colRef.orderBy("Timestamp", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<Message> options = new FirestoreRecyclerOptions.Builder<Message>()
.setQuery(query, Message.class)
.build();
adapter = new MessageRecyclerAdapter(options);
RecyclerView recyclerView = findViewById(R.id.recycler_message_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_PICK && resultCode == RESULT_OK) {
String imageUri = data.getDataString();
CropImage.activity(Uri.parse(imageUri))
.setAspectRatio(1, 1)
.start(this);
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
final String current_user_id = mCurrentUser.getUid();
final String randomName = UUID.randomUUID().toString();
final StorageReference filepath = storageReference.child("chat_photos").child(randomName + ".JPG");
filepath.putFile(resultUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
final String downloadUrl = uri.toString();
Map<String, String> newImageUrl = new HashMap<>();
newImageUrl.put("image", downloadUrl);
firebaseFirestore.collection("Messages").document(TESTmessagedoc).collection("chats")
.add(newImageUrl)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
#Override
public void onSuccess(DocumentReference documentReference) {
/*
filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
//The download url
final String downloadUrl =
uri.toString();
Log.d("tag", downloadUrl);
if (!downloadUrl.equals("default")) {
// I changed this to glide since i thought picasso was the problem.
// Picasso still should work. Glide is recommended by google tho
Glide.with(getApplicationContext()).load(downloadUrl).into(chatProfile);
}
}
});
*/
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(getApplicationContext(), "There was some error in saving Changes.", Toast.LENGTH_LONG).show();
}
});
}
});
}
});
}
}
}}
In the model class i have added getmChatImageURL
In the MessageRecyclerAddapter i added setChatImage
where i am stuck (i think) is in my MessageRecyclerAdapter on my onBindViewHolder i need to set it but i am unsure on how to go about it because i dont know how i would load it into the drawable
Update:
In my MessageRecyclerAdapter i added this
#Override
protected void onBindViewHolder(#NonNull MessageHolder holder, int position, #NonNull Message model) {
holder.ChatMessage.setText(model.getChatMessage());
holder.setChatImage(model.getmChatImageURL());
}
the app run but when i go to upload a image to firestore and the app attempts to display it i get this in the log
W/Glide: Load failed for null with size [0x0]
class com.bumptech.glide.load.engine.GlideException: Received null model
the image is being stored in firestore as a url so i believe the error has something to do with the adapter setup not sure though.
Update 2
I think im on the right track
i changed my adapter class
MessageRecyclerAdapter:
public class MessageRecyclerAdapter extends FirestoreRecyclerAdapter<Message, MessageRecyclerAdapter.MessageHolder> {
private Context mcontext;
public MessageRecyclerAdapter(#NonNull FirestoreRecyclerOptions<Message> options ) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull MessageHolder holder, int position, #NonNull Message model) {
holder.ChatMessage.setText(model.getChatMessage());
Glide.with(mcontext.getApplicationContext())
.load(model.getmChatImageURL())
.into(holder.ChatMessageImage);
}
#NonNull
#Override
public MessageHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.message_item, viewGroup, false);
return new MessageHolder(v);
}
class MessageHolder extends RecyclerView.ViewHolder{
TextView ChatMessage;
CircleImageView MessageProfilePicture;
ImageView ChatMessageImage;
public MessageHolder(#NonNull View itemView) {
super(itemView);
ChatMessage = itemView.findViewById(R.id.Message_Chat_Content);
MessageProfilePicture = itemView.findViewById(R.id.Chat_Profile_picture);
ChatMessageImage = itemView.findViewById(R.id.chat_Message_image);
}
}
}
i added the glide set image into the onbind the error i am facing now when running the code is
android.content.Context android.content.Context.getApplicationContext()' on a null object reference
I also tried
Glide.with(holder.ChatMessageImage.getContext())
.load(model.getmChatImageURL())
.into(holder.ChatMessageImage);
the app runs i am able to successfully upload an image to firestore but then glide throws
W/Glide: Load failed for null with size [0x0]
class com.bumptech.glide.load.engine.GlideException: Received null model
im unsure on how to debug this because when i hook the debugger up to the glide code in the onbind it triggers as soon as the activity is launched
If you already know the imageUrl then just pass it in like this
#Override
protected void onBindViewHolder(#NonNull MessageHolder holder, int position, #NonNull Message model) {
holder.ChatMessage.setText(model.getChatMessage());
holder.setChatImage(imageUrl)
}
I got a source code of a food delivery app from git. he is parsing a website and displaying the menu items, I guess to see the
instead of this I have created a fire-base database and stored one food item for testing
I want to display my item from the fire-base to the app menu I will show the code of all food item fragment,
package com.example.guanzhuli.foody.HomePage.fragment;
public class AllTabFragment extends Fragment {
private String baseUrl = "http://rjtmobile.com/ansari/fos/fosapp/fos_food_loc.php?city=";
private String TAG = "ALLFOOD";
private StorageReference mStorageRef;
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("menu");
ArrayList<Food> foods = new ArrayList<>();
private RecyclerView mRecyclerView;
private AllFoodAdapter adapter;
private RecyclerView.LayoutManager layoutManager;
String foodName;
public AllTabFragment() {
// 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_all_tab, container, false);
mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
foodName = snapshot.child("name").getValue().toString();
String foodPrice = snapshot.child("prize").getValue().toString();
Toast.makeText(getActivity(), foodName, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
// Request Data From Web Service
if (foods.size() == 0) {
objRequestMethod();
}
mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerview_all);
mRecyclerView.setHasFixedSize(false);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
adapter = new AllFoodAdapter(getActivity(), foods);
adapter.setOnItemClickListener(new AllFoodAdapter.OnRecyclerViewItemClickListener() {
#Override
public void onItemClick(View view, String data) {
Bundle itemInfo = new Bundle();
for (int i = 0; i < foods.size(); i++) {
if (foods.get(i).getId() == Integer.valueOf(data)) {
itemInfo.putInt("foodId", foods.get(i).getId());
itemInfo.putString("foodName", foods.get(i).getName());
// itemInfo.putString("foodName", foodName);
itemInfo.putString("foodCat", foods.get(i).getCategory());
itemInfo.putString("foodRec", foods.get(i).getRecepiee());
itemInfo.putDouble("foodPrice", foods.get(i).getPrice());
itemInfo.putString("foodImage", foods.get(i).getImageUrl());
break;
}
}
FoodDetailFragment foodDetailFragment = new FoodDetailFragment();
foodDetailFragment.setArguments(itemInfo);
getActivity().getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(R.anim.fade_in, R.anim.fade_out, R.anim.fade_in, R.anim.fade_out)
.replace(R.id.main_fragment_container, foodDetailFragment)
.addToBackStack(AllTabFragment.class.getName())
.commit();
}
});
mRecyclerView.setAdapter(adapter);
return view;
}
private void objRequestMethod() {
HomePageActivity.showPDialog();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, buildUrl(), null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject jsonObject) {
Log.d(TAG, jsonObject.toString());
try {
JSONArray foodsJsonArr = jsonObject.getJSONArray("Food");
for (int i = 0; i < foodsJsonArr.length(); i++) {
JSONObject c = foodsJsonArr.getJSONObject(i);
String id = c.getString("FoodId");
String name = c.getString("FoodName");
String recepiee = c.getString("FoodRecepiee");
String price = c.getString("FoodPrice");
String category = c.getString("FoodCategory");
String thumb = c.getString("FoodThumb");
final Food curFood = new Food();
curFood.setCategory(category);
curFood.setName(name);
curFood.setRecepiee(recepiee);
curFood.setPrice(Double.valueOf(price));
curFood.setId(Integer.valueOf(id));
curFood.setImageUrl(thumb);
foods.add(curFood);
// Log.e("Current Food", curFood.getName());
ImageLoader imageLoader = VolleyController.getInstance().getImageLoader();
imageLoader.get(thumb, new ImageLoader.ImageListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Image Load Error: " + error.getMessage());
}
#Override
public void onResponse(ImageLoader.ImageContainer response, boolean arg1) {
if (response.getBitmap() != null) {
curFood.setImage(response.getBitmap());
// Log.e("SET IMAGE", curFood.getName());
adapter.notifyData(foods);
}
}
});
foods.get(i).setImage(curFood.getImage());
}
} catch (Exception e) {
System.out.println(e);
}
HomePageActivity.disPDialog();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
VolleyLog.d(TAG, "ERROR" + volleyError.getMessage());
Toast.makeText(getActivity(), volleyError.getMessage(),
Toast.LENGTH_SHORT).show();
HomePageActivity.disPDialog();
}
});
VolleyController.getInstance().addToRequestQueue(jsonObjReq);
}
private String buildUrl() {
return baseUrl + HomePageActivity.City;
}
}
I got food name from fire-base and stored in the string called "foodname" Now I want to display it in the menu, how can I do it?
if my question is wrong please forgive me, please help me
package com.example.guanzhuli.foody.HomePage.adapter;
public class AllFoodAdapter extends RecyclerView.Adapter<AllHolder> implements
View.OnClickListener {
private Context mContext;
ArrayList<Food> foods;
public String TAG = "ALLFOOD";
//
// public AllFoodAdapter(Context context) {
// mContext = context;
// }
public AllFoodAdapter(Context context, ArrayList<Food> foods) {
mContext = context;
this.foods = foods;
}
#Override
public AllHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.cardview_food, parent, false);
AllHolder allHolder = new AllHolder(v);
v.setOnClickListener(this);
return allHolder;
}
#Override
public void onBindViewHolder(AllHolder holder, int position) {
holder.mTextId.setText(String.valueOf(foods.get(position).getId()));
holder.mTextName.setText(foods.get(position).getName());
holder.mTextPrice.setText(String.valueOf(foods.get(position).getPrice()));
holder.mTextCategory.setText(foods.get(position).getCategory());
holder.mImageView.setImageBitmap(foods.get(position).getImage());
holder.itemView.setTag(foods.get(position).getId());
}
#Override
public int getItemCount() {
return foods.size();
}
public void notifyData(ArrayList<Food> foods) {
// Log.d("notifyData ", foods.size() + "");
this.foods = foods;
notifyDataSetChanged();
}
public static interface OnRecyclerViewItemClickListener {
void onItemClick(View view, String data);
}
private OnRecyclerViewItemClickListener mOnItemClickListener = null;
public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
this.mOnItemClickListener = listener;
}
#Override
public void onClick(View view) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(view, String.valueOf(view.getTag()));
} else {
Log.e("CLICK", "ERROR");
}
}
}
class AllHolder extends RecyclerView.ViewHolder {
NetworkImageView mImage;
ImageView mImageView;
TextView mTextId, mTextName, mTextCategory, mTextPrice;
public AllHolder(View itemView) {
super(itemView);
// mImage = (NetworkImageView) itemView.findViewById(R.id.food_img);
mImageView = (ImageView) itemView.findViewById(R.id.food_img);
mTextId = (TextView) itemView.findViewById(R.id.food_id);
mTextName = (TextView) itemView.findViewById(R.id.food_name);
mTextPrice = (TextView) itemView.findViewById(R.id.food_price);
mTextId = (TextView) itemView.findViewById(R.id.food_id);
mTextCategory = (TextView) itemView.findViewById(R.id.food_category);
}
}
Please help me, because it is an important project for me
If you want to display the strings you have received from firebase in a new activity as a list, you can declare an ArrayList and then add those strings to it and then with an adapter set it to display.
In code it looks something like this:
private ArrayList array;
private ListView listView;
private FirebaseAuth mAuth = FirebaseAuth.getInstance();
private String curName;
//set them
listView = findViewById(R.id.list1);
array = new ArrayList<String>();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("menu");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
array.add(ds.child("name").getValue(String.class));
}
ArrayAdapter adapter = new ArrayAdapter(Main6Activity.this, android.R.layout.simple_list_item_1, array);
listView.setAdapter(adapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
stackoverflow was not conceived to get complete code solutions. We can only help you on some point. Other than that, you need to think to use the Food class for read and write values on your db. When you retrive the values, get it as a Food object casting it by method dataSnapshot.getValue("object class");...
In your case you need a code like this:
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Food food = snapshot.getValue(Food.class);
//here you need to add the food object to a list and after the for diplay it with adapter.
//for example foodsList.add(food);
}
foodsList.setAdapter(myAdepter); //ecc
}
If you need help, please tell us more
You seem to be loading the food items from the database, and reading the values from them. All that's left to do, is add each item to the foods array, and tell the adapter that its data has changed:
mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String foodName = snapshot.child("name").getValue(String.class);
String foodPrice = snapshot.child("prize").getValue(String.class);
Food food = new Food();
food.setName(name);
food.setPrice(Double.valueOf(price));
foods.add(food);
}
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
I upload some html text (which contains img tags) to Firestore(images are uploaded to firebase storage). When i try to get these html data to display onto a webview i only get text, no images
Here is the code i used to upload HTML to FireStore
storageReference.child(System.currentTimeMillis() + ".png").putFile(file_uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
post_img_url = String.valueOf(taskSnapshot.getDownloadUrl());
String HTML = editor.getContentAsHTML().replace(uuid_, post_img_url);
Question_model question_model = new Question_model(mAuth.getUid(), HTML);
mFireStoredbReference.collection("Questions").add(question_model).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
#Override
public void onComplete(#NonNull Task<DocumentReference> task) {
Toast.makeText(Forum.this, "Question added", Toast.LENGTH_SHORT).show();
startActivity(new Intent(Forum.this, ListQuestions.class));
finish();
}
});
}
});
And i try to take this data from the Questions collection
firebaseFirestore.collection("Questions").addSnapshotListener(new EventListener<QuerySnapshot>() {
#Override
public void onEvent(#Nullable QuerySnapshot queryDocumentSnapshots, #Nullable FirebaseFirestoreException e) {
if (e instanceof com.google.firebase.firestore.FirebaseFirestoreException) {
// Log.d(TAG, "onEvent: The error is " + e);
} else {
try {
for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
if (doc.getType() == DocumentChange.Type.ADDED) {
Question_model model = doc.getDocument().toObject(Question_model.class);
question_modelList.add(model);
questionAdapter.notifyDataSetChanged();
}
}
} catch (Exception e2) {
}
}
}
});
Here is my adapter for the recyclerview
public class QuestionAdapter extends RecyclerView.Adapter<QuestionAdapter.MyViewHolder> {
Context ctx;
FirebaseFirestore mFirebaseFirestore;
private List<Question_model> question_modelList;
public QuestionAdapter(List<Question_model> question_modelList, Context ctx) {
this.question_modelList = question_modelList;
this.ctx = ctx;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.question_individual, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
mFirebaseFirestore = FirebaseFirestore.getInstance();
String user_id = question_modelList.get(position).getUser_id();
mFirebaseFirestore.collection("users").document(user_id).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
holder.user_name.setText(task.getResult().getString("user_name"));
} else {
}
}
});
Log.d("Asis", "onBindViewHolder: "+question_modelList.get(position).getContent_html());
holder.question.loadData(question_modelList.get(position).getContent_html(),"text/html","utf-8");
}
#Override
public int getItemCount() {
return question_modelList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView user_name;
WebView question;
public MyViewHolder(View itemView) {
super(itemView);
user_name = itemView.findViewById(R.id.user_name);
question = itemView.findViewById(R.id.question);
question.getSettings().setJavaScriptEnabled(true);
question.getSettings().setLoadsImagesAutomatically(true);
question.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
question.getSettings().setDomStorageEnabled(true);
}
}
}
you can use Glide to easily download images from Firebase storage.First, add FirebaseUI to your app/build.gradle:
dependencies {
// FirebaseUI Storage only
implementation 'com.firebaseui:firebase-ui-storage:0.6.0'}
Then you can load images directly from Storage:
Glide.with(this /* context */)
.using(new FirebaseImageLoader())
.load(/*storageReference*/)
.into(/*yourView*/);
I have an adapter class where I am able to load all video from Firebase. But the problem is I can't load both, image and video in one RecyclerView
See what I have done:
I am using toro library to auto play video if your wondering and ExoPlayer to play video
This is my adapter class
public class MainFeedAdapter extends RecyclerView.Adapter<TestAdapter.MyViewHolder> {
private static final String TAG = "MainfeedAdapter";
private List<Photo> moviesList;
private DatabaseReference mReference;
private Context mContext;
private String currentUsername = "";
private int mLayoutResource;
private LayoutInflater mInflater;
private Photo photo;
private MyViewHolder mHolder;
public class MyViewHolder extends RecyclerView.ViewHolder implements ToroPlayer{
static final int LAYOUT_RES = R.layout.main_list;
private ExoPlayerViewHelper helper;
private CircleImageView profile_image;
//private String likeString;
private TextView username,time,caption,likes,comment,stars;
private SquareImageView image;
private LikeButton mHeart,Star;
private UserAccountSettings userAccountSettings = new UserAccountSettings();
private User user = new User();
private StringBuilder users;
private String mLIkeString;
private String mStarString;
private boolean likeByCurrentUSer;
private boolean starbycurrentuser;
private DatabaseReference mNotification;
private ImageView commentBubble;
private Uri mediaUri;
#BindView(R.id.main_post_image2)
PlayerView playerView;
private CardView video;
public MyViewHolder(View view) {
super(view);
profile_image = (CircleImageView)view.findViewById(R.id.post_profile_photo);
username = (TextView) view.findViewById(R.id.main_username);
time = (TextView) view.findViewById(R.id.main_image_time_posted);
caption = (TextView) view.findViewById(R.id.main_image_caption);
likes = (TextView) view.findViewById(R.id.main_likes);
comment = (TextView) view.findViewById(R.id.main_showcomments);
image = (SquareImageView) view.findViewById(R.id.main_post_image);
mHeart = (LikeButton) view.findViewById(R.id.main_heart);
Star = (LikeButton) view.findViewById(R.id.main_star);
stars= (TextView)view.findViewById(R.id.stars);
commentBubble = (ImageView)view.findViewById(R.id.main_comments) ;
mNotification = FirebaseDatabase.getInstance().getReference().child("notification");
mReference = FirebaseDatabase.getInstance().getReference();
ButterKnife.bind(this, itemView);
video = (CardView)view.findViewById(R.id.video_posts);
}
#NonNull
#Override
public View getPlayerView() {
return playerView;
}
#NonNull
#Override
public PlaybackInfo getCurrentPlaybackInfo() {
return helper != null ? helper.getLatestPlaybackInfo() : new PlaybackInfo();
}
#Override
public void initialize(#NonNull Container container, #Nullable PlaybackInfo playbackInfo) {
if (helper == null) {
helper = new SimpleExoPlayerViewHelper(container, this, mediaUri);
}
helper.initialize(playbackInfo);
}
// called from Adapter to setup the media
void bind( Uri item, List<Photo> payloads) {
if (item != null) {
mediaUri = item;
}else {
video.setVisibility(View.GONE);
}
}
#Override
public void play() {
if (helper != null) helper.play();
}
#Override
public void pause() {
if (helper != null) helper.pause();
}
#Override
public boolean isPlaying() {
return helper != null && helper.isPlaying();
}
#Override
public void release() {
if (helper != null) {
helper.release();
helper = null;
}
}
#Override
public boolean wantsToPlay() {
return ToroUtil.visibleAreaOffset(this, itemView.getParent()) >= 0.85;
}
#Override
public int getPlayerOrder() {
return getAdapterPosition();
}
#Override
public void onSettled(Container container) {
}
}
public TestAdapter(List<Photo> moviesList) {
this.moviesList = moviesList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.main_list, parent, false);
mContext = parent.getContext();
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
photo = moviesList.get(position);
mHolder = holder;
holder.users = new StringBuilder();
getCurrentUsername();
getLikesString(mHolder);
getStarString(mHolder);
holder.bind(Uri.parse(photo.getVideo_path()),moviesList);
//get the profile image and username
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(mContext.getString(R.string.dbname_user_account_settings))
.orderByChild(mContext.getString(R.string.field_user_id))
.equalTo(getItem(position).getUser_id());
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
// currentUsername = singleSnapshot.getValue(UserAccountSettings.class).getUsername();
Log.d(TAG, "onDataChange: found user: "
+ singleSnapshot.getValue(UserAccountSettings.class).getUsername());
holder.username.setText(singleSnapshot.getValue(UserAccountSettings.class).getUsername());
holder.username.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: navigating to profile of: " +
holder.user.getUsername());
Intent intent = new Intent(mContext, Profile_Activity.class);
intent.putExtra(mContext.getString(R.string.calling_activity),
mContext.getString(R.string.home_activity));
intent.putExtra(mContext.getString(R.string.intent_user), holder.user);
mContext.startActivity(intent);
}
});
imageLoader.displayImage(singleSnapshot.getValue(UserAccountSettings.class).getProfile_photo(),
holder.profile_image);
holder.profile_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: navigating to profile of: " +
holder.user.getUsername());
Intent intent = new Intent(mContext, Profile_Activity.class);
intent.putExtra(mContext.getString(R.string.calling_activity),
mContext.getString(R.string.home_activity));
intent.putExtra(mContext.getString(R.string.intent_user), holder.user);
mContext.startActivity(intent);
}
});
holder.userAccountSettings = singleSnapshot.getValue(UserAccountSettings.class);
holder.comment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((HomeActivity)mContext).onCommentThreadSelected(getItem(position),mContext.getString(R.string.home_activity));
//another thing?
((HomeActivity)mContext).hideLayout();
}
});
holder.commentBubble.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((HomeActivity)mContext).onCommentThreadSelected(getItem(position),mContext.getString(R.string.home_activity));
//another thing?
((HomeActivity)mContext).hideLayout();
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Query userQuery = mReference
.child(mContext.getString(R.string.dbname_users))
.orderByChild(mContext.getString(R.string.field_user_id))
.equalTo(getItem(position).getUser_id());
userQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
Log.d(TAG, "onDataChange: found user: " +
singleSnapshot.getValue(User.class).getUsername());
holder.user = singleSnapshot.getValue(User.class);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private boolean rechedendoflist(int position){
return position == getItemCount() -1;\
}
#Override
public int getItemCount() {
return moviesList.size();
}
public Photo getItem(int position) {
return moviesList.get(position);
}
private void getCurrentUsername(){
Log.d(TAG, "getCurrentUser: ");
Log.d(TAG, "getCurrentUsername: retrieving user account settings");
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(mContext.getString(R.string.dbname_users))
.orderByChild(mContext.getString(R.string.field_user_id))
.equalTo(FirebaseAuth.getInstance().getCurrentUser().getUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
currentUsername = singleSnapshot.getValue(UserAccountSettings.class).getUsername();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Please help me guys!. Thanks in Advance!.
TheRecyclerView has ability to show items in multiple types.
In your case you can define two ViewType. one for Images and another for Videos.
Search about RecyclerView with multiple view types.
Take a look at here and this.
Also here is a question and answers about it.
I'm facing an issue.
I'm getting photos through UID,
but issue is in firebase database where images are replacing
if I use the push then application didn't get images from firebase
Firebase Database Image
If I remove push from database then images will be show, but as you can see that images will overlap everytime user upload image
Here is my code for onActivityResult
#Override
protected void onActivityResult ( int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
final FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK) {
TitleName=title.getText().toString();
progressDialog.setMessage("Uploading .....");
progressDialog.show();
Uri uri = data.getData();
StorageReference filePath = storageReference.child("Photos").child(currentFirebaseUser.getUid()).child(uri.getLastPathSegment());
filePath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadPath = taskSnapshot.getDownloadUrl();
Glide.with(MainActivity.this).load(downloadPath).centerCrop().into(imageView);
Toast.makeText(getApplicationContext(), "DONE" + TitleName, Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
//creating the upload object to store uploaded image details
Upload upload = new Upload(taskSnapshot.getDownloadUrl().toString(), TitleName);
//adding an upload to firebase database
// String uploadId = mDatabase.push().getKey();
mDatabase.child(currentFirebaseUser.getUid())***.push().***setValue(upload);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
If i remove push from
mDatabase.child(currentFirebaseUser.getUid())***.push().***setValue(upload);
then images will be show on recyclerView.
Any suggestions?
ShowImagesActivity
public class ShowImagesActivity extends AppCompatActivity {
//recyclerview object
private RecyclerView recyclerView;
//adapter object
private RecyclerView.Adapter adapter;
//database reference
private DatabaseReference mDatabase;
//progress dialog
private ProgressDialog progressDialog;
//list to hold all the uploaded images
private List<Upload> uploads;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recycler_view);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
progressDialog = new ProgressDialog(this);
uploads = new ArrayList<>();
//displaying progress dialog while fetching images
progressDialog.setMessage("Loading your memories ...");
progressDialog.show();
mDatabase = FirebaseDatabase.getInstance().getReference(Constants.DATABASE_PATH_UPLOADS);
//adding an event listener to fetch values
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
//dismissing the progress dialog
progressDialog.dismiss();
//iterating through all the values in database
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Upload upload = postSnapshot.getValue(Upload.class);
uploads.add(upload);
}
//creating adapter
adapter = new MyAdapter(getApplicationContext(), uploads);
//adding adapter to recyclerview
recyclerView.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
progressDialog.dismiss();
}
});
}
}
Constants.java
public static final String DATABASE_PATH_UPLOADS = "uploads";
public static final String STORAGE_PATH_UPLOADS = "uploads/";
Adapter.java
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private Context context;
private List<Upload> uploads;
public MyAdapter(Context context, List<Upload> uploads) {
this.uploads = uploads;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_images, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Upload upload = uploads.get(position);
holder.textViewName.setText(upload.getName());
Glide.with(context).load(upload.getUrl()).into(holder.imageView);
}
#Override
public int getItemCount() {
return uploads.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView textViewName;
public ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
textViewName = (TextView) itemView.findViewById(R.id.textViewName);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
}
}
}
upload.java
public class Upload{
public String name;
public String url;
// Default constructor required for calls to
// DataSnapshot.getValue(User.class)
public Upload() {
}
public Upload( String url, String name) {
this.name = name;
this.url= url;
}
public String getName() {
return name;
}
public String getUrl() {
return url;
}