FirebaseListAdapter什么都不显示。没有任何错误

时间:2018-02-14 06:43:52

标签: android listview firebase firebaseui

当我使用FirebaseListAdapter时,它似乎返回一个空列表,没有附加错误。

是的,我加倍检查我的firebaseDatabase是不是空的,我还提出转储值只是为了检查,仍然没有..请帮助我:(

PrivateChat活动:

import android.database.DataSetObserver;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.ui.database.FirebaseListAdapter;
import com.firebase.ui.database.FirebaseListOptions;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;

import java.sql.Time;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class PrivateChatActivity extends AppCompatActivity implements View.OnClickListener {

// Variables
private String chatRoomName;
private ArrayAdapter<ChatMessage> listOfMessages;

// UI
private ListView listView;
private FloatingActionButton btnSend;
private EditText etInputText;

// Firebase
private FirebaseAuth auth;
private DatabaseReference mRefRead;
private DatabaseReference mRefWrite;

//
private FirebaseListAdapter<ChatMessage> adapter;
//



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

    // UI
    listView = (ListView)findViewById(R.id.list_of_messages);
    etInputText = (EditText)findViewById(R.id.text_input);
    btnSend = (FloatingActionButton)findViewById(R.id.fab);
    btnSend.setOnClickListener(this);

    // Variables
    listOfMessages = new ArrayAdapter<ChatMessage>(this, R.layout.item_chat_message);

    // Firebase
    auth = FirebaseAuth.getInstance();
    chatRoomName = "ChatRoomOf: " + auth.getCurrentUser().getUid() + " + " + "NULL";

    displayChatMessages();
}

private void displayChatMessages() {
    Query query = FirebaseDatabase.getInstance().getReference().child("Chats").child(chatRoomName);
    FirebaseListOptions<ChatMessage> options = new FirebaseListOptions.Builder<ChatMessage>()
            .setQuery(query, ChatMessage.class)
            .setLayout(R.layout.item_chat_message)
            .build();

    adapter = new FirebaseListAdapter<ChatMessage>(options) {
        @Override
        protected void populateView(View v, final ChatMessage model, int position) {
            //Get reference to the views of list_item_chat_messages.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("HH:mm", model.getMessageTime()));
        }
    };
    listView.setAdapter(adapter);
}


@Override
public void onClick(View v) {
    if(v == btnSend){
            Firebasedatabase.getInstance().getReference().child("Chats").child(chatRoomName.push().setValue(new ChatMessage(etInput.getText().toString(), auth.getCurrentUser().getEmail()));
            etInputText.setText("");
        }
    }
}

ChatMessage活动:

import java.util.Date;

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) {
        this.messageText = messageText;
    }

    public String getMessageUser() {
        return messageUser;
    }

    public void setMessageUser(String messageUser) {
        this.messageUser = messageUser;
    }

    public long getMessageTime() {
        return messageTime;
    }

    public void setMessageTime(long messageTime) {
        this.messageTime = messageTime;
    }
}

item_chat_message.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layoutDirection="ltr"
    android:descendantFocusability="blocksDescendants">

    <TextView
        android:id="@+id/message_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@id/message_user"
        android:layout_marginTop="5dp"
        android:text="User text here"
        android:padding="10dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/message_user"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:textStyle="normal|bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/message_time"
        android:layout_alignParentBottom="@+id/message_user"
        android:layout_alignParentEnd="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

Grandle app模块:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "..."
        minSdkVersion 17
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.0.2'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.google.firebase:firebase-database:11.8.0'
    implementation 'com.google.firebase:firebase-auth:11.8.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    compile 'com.android.support:design:27.0.2'
    compile 'com.google.android.gms:play-services-maps:11.8.0'

    //
    implementation 'com.firebaseui:firebase-ui-database:3.1.3'
    //
}



apply plugin: 'com.google.gms.google-services'

0 个答案:

没有答案