listview在启动活动后没有显示我的消息

时间:2018-06-11 12:04:16

标签: android firebase-realtime-database firebaseui

谁能帮助我? listview在启动活动后没有显示我的消息,我正在尝试创建聊天应用程序,我已经成功创建了聊天应用程序,然后在广告中添加了一些依赖项,它开始在适配器代码中给我错误。 首先我认为列表适配器中的问题请检查我的主要活动和其他类

 package com.intraday.geeks;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.text.format.DateFormat;

import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.database.FirebaseListAdapter;
import com.firebase.ui.database.FirebaseListOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;


public class MainActivity extends AppCompatActivity {

    private static int SIGN_IN_REQUEST_CODE = 1;
    private FirebaseListAdapter<ChatMessage> adapter;
    RelativeLayout activity_Main;
    FloatingActionButton fab;


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == R.id.menu_sign_out)
        {
            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
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu,menu);
        return true;
    }

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

    }



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

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

            }
        });




        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();
            //Load content
            displayChatMessage();
        }
    }
    private void displayChatMessage() {
        ListView listOfMessage = (ListView)findViewById(R.id.list_of_message);
        Query query = FirebaseDatabase.getInstance().getReference().child("chats");
        FirebaseListOptions<ChatMessage> options =
                new FirebaseListOptions.Builder<ChatMessage>()
                        .setQuery(query, ChatMessage.class)
                        .setLayout(android.R.layout.simple_list_item_1)
                        .build();
        adapter = new FirebaseListAdapter<ChatMessage>(options)
        {
            @Override
            protected void populateView(View v, ChatMessage model, int position) {

                //Get references to the views of list_item.xml
                TextView messageText, messageUser, messageTime;
                messageText = (TextView) v.findViewById(R.id.message_Text);
                messageUser = (TextView) v.findViewById(R.id.message_user);
                messageTime = (TextView) v.findViewById(R.id.message_time);

                messageText.setText(model.getMessageText());
                messageUser.setText(model.getMessageuser());
                messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)", model.getMessageTime()));

            }
        };
        listOfMessage.setAdapter(adapter);
    }
    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }


    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();

    }
}

这是我的 chatMessage类

package com.intraday.geeks;

import java.util.Date;

/**
 * Created by Administrator on 6/8/2018.
 */

public class ChatMessage {

    private String MessageText;
    private String Messageuser;
    private Long MessageTime;

    public ChatMessage(String messageText, String messageuser) {
        this.MessageText = messageText;
        this.Messageuser = messageuser;
        MessageTime= new Date().getTime();
    }

    public ChatMessage() {
    }

    public String getMessageText() {
        return MessageText;
    }

    public void setMessageText(String messageText) {
        MessageText = messageText;
    }

    public String getMessageuser() {
        return Messageuser;
    }

    public void setMessageuser(String messageuser) {
        Messageuser = messageuser;
    }

    public Long getMessageTime() {
        return MessageTime;
    }

    public void setMessageTime(Long messageTime) {
        MessageTime = messageTime;
    }
}

这是我的 MainActivity Layout

    <?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:id="@+id/layoutmain"
        android:layout_width="match_parent"
        android:background="@drawable/gg"
        android:layout_height="match_parent"
        tools:context="com.intraday.geeks.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_alignParentEnd="true"
            app:fabSize="mini"/>

        <EditText
            android:id="@+id/input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/textInputLayout"
            android:layout_alignParentStart="true"
            android:hint="message"
            android:textColor="#ffff"
            android:textColorHint="#ffff"
            />

        <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_alignParentLeft="true"
            android:id="@+id/textInputLayout" />

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



    </RelativeLayout>



**this is my list_iteam . 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:id="@+id/scroll"
    android:background="@drawable/border_style"
    android:orientation="vertical">


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="10dp"
        android:id="@+id/message_user"
        android:visibility="invisible"/>


    <TextView
        android:id="@+id/message_time"
        android:layout_width="fill_parent"
        android:layout_height="18dp"
        android:layout_alignParentBottom="@id/message_Text"
        android:textColor="#ffff"
        android:textSize="15dp"
        />

    <TextView
        android:id="@+id/message_Text"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/message_time"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textColor="#ffff"
        android:textSize="20sp"
        android:textStyle="bold"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="90dp" />


</RelativeLayout>

这是我的Menifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.intraday.geeks">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <!-- This meta-data tag is required to use Google Play Services. -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <!-- Include the AdActivity configChanges and theme. -->
        <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />
    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

添加此

@Override
protected void onStart() {
    super.onStart();
    adapter.startListening();
}


@Override
protected void onStop() {
    super.onStop();
    adapter.stopListening();

}