0

Main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:id="@+id/activity_main"
tools:context="com.example.syafiq.mychatapp.MainActivity">



<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:src="@drawable/ic_send"
    android:id="@+id/fab"
    android:tint="@android:color/white"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    app:fabSize="mini"
    />

<android.support.design.widget.TextInputLayout

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/fab"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    >
    <EditText
        android:id="@+id/messageinput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/fab"
        android:layout_centerHorizontal="true"
        android:hint="Message..." />
</android.support.design.widget.TextInputLayout>


<ListView
    android:id="@+id/list_of_message"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:layout_above="@+id/fab"
    android:dividerHeight="16dp"
    android:divider="@android:color/transparent"
    android:layout_marginBottom="16dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</ListView>

Maincode

private static int SIGN_IN_REQUEST_CODE =1;
//private List<ChatMessage> list = new ArrayList<ChatMessage>();
FirebaseListAdapter<ChatMessage> adapter;
RelativeLayout activity_main;
FloatingActionButton fab;

FirebaseDatabase db = FirebaseDatabase.getInstance();
DatabaseReference myRef = db.getReference("message");


@Override
public boolean onOptionsItemSelected(MenuItem item)
{

    if (item.getItemId() == R.id.menu_signout)
    {
        AuthUI
            .getInstance()
            .signOut(this)
            .addOnCompleteListener(
                new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task)
                    {
                        Snackbar
                            .make(
                                activity_main,
                                "You have been signed out.", 
                                Snackbar.LENGTH_SHORT
                            ).show()
                        ;
                        finish();
                    }
                }
            )
        ;
    }
    return true;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode==SIGN_IN_REQUEST_CODE)
    {
        if (resultCode==RESULT_OK)
        {
            Snackbar
                .make(
                    activity_main,
                    "Successfully signed in!",
                    Snackbar.LENGTH_SHORT
                ).show()
            ;
            displayChatMessage();
        }
        else
        {
            Snackbar
                .make(
                    activity_main,
                    "We couldn't sign you in. Please try again lter!", 
                    Snackbar.LENGTH_SHORT
                ).show()
            ;
            finish();
        }
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(R.menu.main_menu,menu);
    return true;
}

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    activity_main = (RelativeLayout) findViewById(R.id.activity_main);
    fab = (FloatingActionButton) findViewById(R.id.fab);
    fab
        .setOnClickListener(
            new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    EditText input = (EditText) findViewById(R.id.messageinput);
                    FirebaseDatabase
                        .getInstance()
                        .getReference()
                        .push()
                        .setValue(
                            new ChatMessage(
                                input.getText().toString(),
                                FirebaseAuth.getInstance().getCurrentUser().getEmail()
                            )
                        )
                    ;

                    input.setText("");

                    displayChatMessage();
                }
            }
        )
    ;

    if (FirebaseAuth.getInstance().getCurrentUser()== null)
    {
        startActivityForResult(
            AuthUI
                .getInstance()
                .createSignInIntentBuilder()
                .build(),
            SIGN_IN_REQUEST_CODE
        );
    }
    else
    {
        Snackbar
            .make(
                activity_main,
                "Welcome " + FirebaseAuth
                    .getInstance()
                    .getCurrentUser()
                    .getEmail(),
                Snackbar.LENGTH_SHORT
            ).show()
        ;
        //displayChatMessage();
    }
}    

DisplaychatMessage() function to display my chat message

private void displayChatMessage() {
    Query query = FirebaseDatabase.getInstance().getReference().child("Chats");
    ListView listofmsgs = (ListView) findViewById(R.id.list_of_message);

    FirebaseListOptions<ChatMessage> options = new FirebaseListOptions
        .Builder<ChatMessage>()
        .setQuery(query, ChatMessage.class)
        .setLayout(R.layout.list_item)
        .build()
    ;
    //adapter.startListening();
    Log.d("ErrorCheck", "1");
    adapter = new FirebaseListAdapter<ChatMessage>(options) {
        @Override protected void populateView(View v, ChatMessage model, int position) {
            //ChatMessage cm = (ChatMessage) model;
            TextView messageText, messageUser, messageTime;
            messageText = (TextView) v.findViewById(R.id.messageinput);
            messageUser = (TextView) v.findViewById(R.id.message_user);
            messageTime = (TextView) v.findViewById(R.id.message_time);

            messageText.setText(model.getMessageText().toString());
            messageUser.setText(model.getMessageUser());
            messageTime
                .setText(
                    android.text.format.DateFormat.format(
                        "dd-mm-yyyy (HH:mm:ss)", 
                        model.getMessageTime()
                    )
                )
            ;                                
            Log.d("ErrorCheck", "2");
        }
    };
    listofmsgs.setAdapter(adapter);
    adapter.startListening();
}

Hi guys, i did this but it doesn't seem like anything appear on my APP. But the then, when i press send, in my database, my chat appears there but again it doesn't appear on my Chat app. I did a debug log. Errorcheck 1, and 2 to see where the code ends. When i checked, looks like the debug log only display up till ErrorCheck 1 and does not display display 2. How do i solve this?

candied_orange
  • 7,036
  • 2
  • 28
  • 62
Syafiq
  • 31
  • 5

1 Answers1

0

You've put ErrorCheck 2 in something called an anonymous class. That's why calling your displayChatMessage() will only log ErrorCheck 1. The code in the anonymous FirebaseListAdapter class you defined will only run when it's populateView() method is called. You don't call that method. You call adapter.startListening(); Something, somewhere, needs to call adapter.populateView() before you'll see ErrorCheck 2 logged.

You likely don't want to call it here. You can call it here just for a test but you should track down what is supposed to be calling it.

According to the docs about FirebaseListAdapter

This class is a generic way of backing an Android ListView with a Firebase location. It handles all of the child events at the given Firebase location. It marshals received data into the given class type. Extend this class and provide an implementation of populateView, which will be given an instance of your list item mLayout and an instance your class that holds your data. Simply populate the view however you like and this class will handle updating the list as the data changes.

So trying changing the data and see if that makes you log ErrorCheck 2

candied_orange
  • 7,036
  • 2
  • 28
  • 62