Click here to Skip to main content
14,926,277 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Helo, im making an app like post/comment in android studio connected with Firebase, it works perfectly but i cant retrieve the profile image from the user who send a message. I have 3 classes for it, a Model class, Adapter and Actrivity... what im doing wrong?

Class Adapter :

<pre>MessagesAdapter(@NonNull Context context, int resource, @NonNull List<Message> objects, ChatActivity chatActivity) {
        super(context, resource, objects);
        this.ctx = context;
        this.messageObjects = (ArrayList<Message>) objects;
        this.dataUpdateAfterMessageDelete = chatActivity;
        mAuth = FirebaseAuth.getInstance();
        currentUserId = mAuth.getCurrentUser().getUid();
        mDatabase = FirebaseDatabase.getInstance().getReference();
 
 
    }
 
 
    @SuppressLint("UseCompatLoadingForDrawables")
    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        firebaseUser = mAuth.getCurrentUser();
        UsersRef = FirebaseDatabase.getInstance().getReference().child("Users");
        UserProfileImageRef = FirebaseStorage.getInstance().getReference().child("profileimage");
        message = getItem(position);
        MessagesAdapter.ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.messages_listview, parent, false);
            viewHolder = new MessagesAdapter.ViewHolder();
            viewHolder.messageTV = convertView.findViewById(R.id.messageTV);
            viewHolder.messageSenderTV = convertView.findViewById(R.id.messageSenderTV);
            viewHolder.messageTimeTV = convertView.findViewById(R.id.messageTimeTV);
            viewHolder.fotoforos = convertView.findViewById(R.id.fotoforos);
            viewHolder.deleteMessageButton = convertView.findViewById(R.id.deleteMessageButton);
            convertView.setTag(viewHolder);
 
        } else {
            viewHolder = (MessagesAdapter.ViewHolder) convertView.getTag();
        }
        if (!message.user_id.equals(Objects.requireNonNull(mAuth.getCurrentUser()).getUid())) {
            viewHolder.deleteMessageButton.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.deleteMessageButton.setVisibility(View.VISIBLE);
        }
        viewHolder.messageSenderTV.setText(message.user_name);
        viewHolder.messageTV.setText(message.message);
        if (message != null) {
            Picasso.with(ctx).load(message.getProfileimage()).placeholder(R.drawable.profilefoto).into(viewHolder.fotoforos);
 
        } else {
            viewHolder.fotoforos.setImageDrawable(ctx.getResources().getDrawable(R.drawable.profilefoto));
        }
        try {
            convertedDate = dateFormat.parse(message.created_time);
            dateFormat.format(new Date());
            String datestre = dateFormat.format(convertedDate.getTime());
            datestre = datestre.replace("-", "/");
         }
        catch (ParseException e) {
        e.printStackTrace();
        }
        viewHolder.messageTimeTV.setText(dateFormat.format(convertedDate));
        viewHolder.deleteMessageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataUpdateAfterMessageDelete.deleteMessage(messageObjects.get(position).message_id);
            }
        });
 
        return convertView;
    }
 
    private static class ViewHolder {
        TextView messageTV, messageSenderTV, messageTimeTV;
        ImageButton deleteMessageButton;
        CircleImageView fotoforos;
    }
 
    public interface DataUpdateAfterMessageDelete {
        void deleteMessage(String message_id);
    }


Activity:

protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_chat);
       setTitle("Chatroom");
 
       threadNameTV = findViewById(R.id.threadNameTV);
       newMessageET = findViewById(R.id.newMessageET);
       homeButton = findViewById(R.id.homeButton);
       sendButton = findViewById(R.id.sendButton);
       messagesLV = findViewById(R.id.messagesLV);
       final View messageview = getLayoutInflater().inflate(R.layout.messages_listview, null);
       messageTV = messageview.findViewById(R.id.messageTV);
       fotoforos = messageview.findViewById(R.id.fotoforos);
 
 
       mAuth = FirebaseAuth.getInstance();
       currentUserId = mAuth.getCurrentUser().getUid();
       user = mAuth.getCurrentUser();
       mDatabase = FirebaseDatabase.getInstance().getReference();
       firebaseUser = mAuth.getCurrentUser();
       UsersRef = FirebaseDatabase.getInstance().getReference().child("Users");
       UserProfileImageRef = FirebaseStorage.getInstance().getReference().child("profileimage");
       googleSignInClient = GoogleSignIn.getClient(ChatActivity.this, GoogleSignInOptions.DEFAULT_SIGN_IN);
 
 
       threadNameTV.setTextColor(Color.parseColor("#000000"));
 
       if (getIntent() != null && getIntent().getExtras() != null) {
           if (getIntent().getExtras().containsKey("messageThreadDetails")) {
               messageThread = (MessageThread) getIntent().getSerializableExtra("messageThreadDetails");
               threadNameTV.setText(messageThread.title);
               getMessages(messageThread.thread_id);
               incrementar();
           }
       } else {
           Toast.makeText(this, "No hay informaciión", Toast.LENGTH_SHORT).show();
       }
 
       homeButton.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               if (user != null) {
                   user = null;
                   mDatabase = null;
                   mAuth = null;
                   Intent intent = new Intent(ChatActivity.this, ThreadsActivity.class);
                   startActivity(intent);
                   finish();
               } else {
                   user = null;
                   mDatabase = null;
                   mAuth = null;
                   Toast.makeText(ChatActivity.this, "Inicia sesion por favor", Toast.LENGTH_SHORT).show();
                   Intent intent = new Intent(ChatActivity.this, IniciarSesion.class);
                   startActivity(intent);
                   finish();
               }
           }
       });
 
       sendButton.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               String message = newMessageET.getText().toString();
               String user_name = user.getDisplayName();
               String profileimage = UsersRef.child(currentUserId).child("profileimage").getRef().toString();
 
               if (message.isEmpty()) {
                   Toast.makeText(ChatActivity.this, "Escribe un mensaje", Toast.LENGTH_SHORT).show();
               } else {
                   addMessage(message, user_name, profileimage, messageThread.thread_id);
               }
           }
       });
 
   }
 
 
   public void addMessage(String message, String user_name, String profileimage, String thread_id) {
       if (user != null) {
           mDatabase.child("Normas").child(thread_id).child("messages").push().setValue(new Message(message, user.getUid(), user_name, profileimage, new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.US).format(new Date())));
           newMessageET.setText("");
           incrementarcomentarios();
       } else {
           Toast.makeText(this, "No has iniciado sesion", Toast.LENGTH_SHORT).show();
       }
   }
 
 
   public void getMessages(String thread_id) {
       mDatabase.child("Normas").child(thread_id).child("messages").addValueEventListener(new ValueEventListener() {
           @Override
           public void onDataChange(DataSnapshot dataSnapshot) {
               messagesList.clear();
               for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()) {
                   Message message = messageSnapshot.getValue(Message.class);
                   if (message != null) {
                       message.message_id = messageSnapshot.getKey();
                       Log.d(TAG, "onDataChange: " + message.toString());
                   }
                   messagesList.add(message);
               }
               messagesAdapter = new MessagesAdapter(ChatActivity.this, R.layout.threads_listview, messagesList, ChatActivity.this);
               messagesLV.setAdapter(messagesAdapter);
           }
 
           @Override
           public void onCancelled(DatabaseError databaseError) {
               Toast.makeText(ChatActivity.this, "ChatActivity: " + databaseError.getMessage(), Toast.LENGTH_SHORT).show();
           }
       });
   }


What I have tried:

I tried to use Glide library and Picasso with same results on both. So i the problem is how im trying to retrieve the data from firebase in Activity i guess...
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900