Click here to Skip to main content
15,036,145 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I cant add all the code but I will explain it very clearly plz bear with me 

**I KNOW I AM ASKING TOO MUCH AND IT IS ANNOYING BUT PLZ HELP ME ILL BE FOREVER IN YOUR DEBT

I STARTED LEARNING CODING RECENTLY MAYBE THATS WHY IM NOT GOOD OR MAYBE IM A LITTLE DUMB BUT I HAVE BEEN AT IT FOR A WEEK NOW I HOPE THAT YOU GUYS CAN HELP ME WITH THIS**

I picked a chat app which had 2 fragments which could be swiped I wanted to add my own fragment in it so I added a global chat app I created my self I didn't use anything from existing code so not to mess with existing I made a global folder and added my java files and layout files in app to make use of it now here is the problem I cant seem to get the current user name no matter what now plz let me explain first i have almost tried all methods plz believe me i have tried almost all methods on diff stackflow and other sites before i posted message here

ill start with how Database looks like in Firebase with a picture
These will help you understand it more i added global chat so the folder of global is mine rest is nothing to do with what i added

[This is picture of what i wana get from Database by it seems when i in my global fragment im not connected with it at all so i cant get it with getCurrentUser and when i use other methods to get it i get blank instead of the palce where name should be when i run my app][1]


[How other Data is saved in Database][2]


[This is how my Data is been saved now for the Fragment i added][3]


  [1]: https://i.stack.imgur.com/PKELr.png
  [2]: https://i.stack.imgur.com/htlpZ.png
  [3]: https://i.stack.imgur.com/m3RsQ.png



now the code i added 

    public class GlobalFragment extends Fragment {

    public static final String ANONYMOUS = "anonymous";
    public static final int DEFAULT_MSG_LENGTH_LIMIT = 1000;

    private ListView mMessageListView;
    private MessageAdapter mMessageAdapter;
    private ProgressBar mProgressBar;
    private ImageButton mPhotoPickerButton;
    private EditText mMessageEditText;
    private Button mSendButton;

    private String mUsername;
    private String myid;

    private FirebaseDatabase mFirebaseDatabase;
    private DatabaseReference mMessagesDatabaseReference;

    private DatabaseReference mRef;

    private ChildEventListener mChildEventListener;
    private FirebaseAuth mFirebaseAuth;
    private FirebaseAuth.AuthStateListener mAuthStateListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_global, container, false);

        /** TODO: Insert all the code from the NumberActivity’s onCreate() method after the setContentView method call */
        mUsername = ANONYMOUS;
        myid = StaticConfig.UID;

        mFirebaseDatabase = FirebaseDatabase.getInstance();
        mFirebaseAuth = FirebaseAuth.getInstance();

        mRef = mFirebaseDatabase.getReference();
        mRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot ds : dataSnapshot.getChildren()){
                    FriendlyMessage uinfo = new FriendlyMessage();
                    uinfo.setName(ds.child(myid).getValue(FriendlyMessage.class).getName());
                    mUsername = uinfo.getName();
                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

        mMessagesDatabaseReference = mFirebaseDatabase.getReference().child("global");

        // Initialize references to views
        mProgressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);
        mMessageListView = (ListView) rootView.findViewById(R.id.messageListView);
        mPhotoPickerButton = (ImageButton) rootView.findViewById(R.id.photoPickerButton);
        mMessageEditText = (EditText) rootView.findViewById(R.id.messageEditText);
        mSendButton = (Button) rootView.findViewById(R.id.sendButton);

        // Initialize message ListView and its adapter
        List<FriendlyMessage> friendlyMessages = new ArrayList<>();
        mMessageAdapter = new MessageAdapter(getActivity(), R.layout.item_message_global, friendlyMessages);
        mMessageListView.setAdapter(mMessageAdapter);

        // Initialize progress bar
        mProgressBar.setVisibility(ProgressBar.INVISIBLE);

        // ImagePickerButton shows an image picker to upload a image for a message
        mPhotoPickerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // TODO: Fire an intent to show an image picker
            }
        });

        // 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)});

        // Send button sends a message and clears the EditText
        mSendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // TODO: Send messages on click
                FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), mUsername, null);
                mMessagesDatabaseReference.push().setValue(friendlyMessage);

                // Clear input box
                mMessageEditText.setText("");
            }
        });
        mChildEventListener = new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                FriendlyMessage friendlyMessage = dataSnapshot.getValue(FriendlyMessage.class);
                mMessageAdapter.add(friendlyMessage);
            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        };
        mMessagesDatabaseReference.addChildEventListener(mChildEventListener);

        return rootView;
    }

**MessageAdapter**

    public class MessageAdapter extends ArrayAdapter<FriendlyMessage> {

    private List<FriendlyMessage> listMessage;

    public MessageAdapter(Context context, int resource, List<FriendlyMessage> objects) {
        super(context, resource, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.item_message_global, parent, false);
        }

        ImageView photoImageView = (ImageView) convertView.findViewById(R.id.photoImageView);
        TextView messageTextView = (TextView) convertView.findViewById(R.id.messageTextView);
        TextView authorTextView = (TextView) convertView.findViewById(R.id.nameTextView);

        FriendlyMessage message = getItem(position);

        boolean isPhoto = message.getPhotoUrl() != null;
        if (isPhoto) {
            messageTextView.setVisibility(View.GONE);
            photoImageView.setVisibility(View.VISIBLE);
            Glide.with(photoImageView.getContext())
                    .load(message.getPhotoUrl())
                    .into(photoImageView);
        } else {
            messageTextView.setVisibility(View.VISIBLE);
            photoImageView.setVisibility(View.GONE);
            messageTextView.setText(message.getText());
        }
        authorTextView.setText(message.getName());

        return convertView;
    }
}

    public class FriendlyMessage {

    private String text;
    private String name;
    private String photoUrl;

    public FriendlyMessage() {
    }

    public FriendlyMessage(String text, String name, String photoUrl) {
        this.text = text;
        this.name = name;
        this.photoUrl = photoUrl;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhotoUrl() {
        return photoUrl;
    }

    public void setPhotoUrl(String photoUrl) {
        this.photoUrl = photoUrl;
    }

}

    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:background="@color/tan_background"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/messageListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/linearLayout"
        android:stackFromBottom="true"
        android:divider="@android:color/transparent"
        android:transcriptMode="alwaysScroll"
        tools:listitem="@layout/item_message_global"/>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:orientation="horizontal"
        android:background="@drawable/chat_send_background_global">

        <ImageButton
            android:id="@+id/photoPickerButton"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:layout_gravity="bottom"
            android:background="@android:drawable/ic_menu_gallery" />

        <EditText
            android:id="@+id/messageEditText"
            android:layout_width="0dp"
            android:maxLines="6"
            android:hint="Type a message"
            android:background="#ffff"
            android:layout_height="33dp"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"/>

        <!--<Button-->
        <!--android:id="@+id/sendButton"-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:layout_gravity="bottom"-->
        <!--android:enabled="false"-->
        <!--android:text="@string/send_button_label"/>-->
        <Button
            android:id="@+id/sendButton"
            android:background="@drawable/ic_send_black_24dp_global"
            android:layout_width="30dp"
            android:layout_height="40dp"
            android:layout_gravity="bottom"
            android:enabled="false"
            android:layout_weight="0.03" />

    </LinearLayout>

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleLarge" />

</RelativeLayout>


    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginStart="@dimen/activity_horizontal_margin"
    android:padding="4dp"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/photoImageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true" />

    <TextView
        android:id="@+id/nameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity=""
        android:textColor="#9f000000"
        android:layout_weight="0"
        android:textAppearance="?android:attr/textAppearanceSmall"
        tools:text="Name" />

    <TextView
        android:id="@+id/messageTextView"
        android:layout_width="wrap_content"
        android:text="Message "
        android:textSize="15dp"
        android:layout_marginRight="@dimen/item_right_margin"
        android:gravity="center_horizontal"
        android:background="@drawable/chat_item_background_global"
        android:textColor="#fff"
        android:padding="7dp"
        android:textAlignment="textStart"
        android:clickable="true"
        android:autoLink="web"
        android:textIsSelectable="true"
        android:textEditPasteWindowLayout="@color/authui_colorActivated"
        android:layout_height="wrap_content"
        tools:ignore="RtlCompat" />

</LinearLayout>


I have a working global chat app which has can send both message and image and i trying to add it little by little but i didn't expect to be stuck here FOR A WEEK i have literally understood where the saying "Hitting your head on the wall came from "

Plz i beg you i have at it for a week but cant get the idotic username i have many methods but still cant get it sometimes printing blank name when i run app  i used even uid to some get it but i feel like crying 

if you want to know in which code i added my code to its this sorry i couldn't add the code directly because its to big i just cant get the concept why people create so many java and layouts....
https://github.com/ashwin-phadke/ZappChatt


What I have tried:

I have tried getting it with Uid and simply getcurrentUser and many other methods I found all over the place for a week
Posted
Comments
David Crow 13-Sep-18 16:45pm
   
This is the problem with downloading someone else's code without having a full understanding of what it is doing and how it is all tied together.

Since you are a "beginner," I suggest starting with a few simpler projects that do not involve: chat, fragments, or Firebase. Once you start getting the hang of things, slowly add to them one piece at a time, rather than trying to do everything at once (with borrowed code that you know little about).

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