数据无法在Firebase Recycler View中显示

时间:2017-10-28 16:38:05

标签: android firebase android-recyclerview recycler-adapter firebaseui

我已经按照几个不同的文档来整理我的第一个Firebase Recycler View:

两个主要的:

Herehere

我正在使用Firebase Recycler View的Message对象(包含在下面)。由于某种原因,数据未显示。从我的日志输出判断,适配器中没有任何东西被调用。以下是我的onCreate()中的活动代码:

public static void countWords () {
    String textFileLocation ="c:\\yourFileLocation";
    String readWords ="";
    ArrayList<String> extractOnlyWordsFromTextFile = new ArrayList<>();
    // excludedSymbols can be extended to whatever you want to exclude from the file 
    String[] excludedSymbols = {" ", "," , "." , "/" , ":" , ";" , "<" , ">", "\n"};
    String readByteCharByChar = "";
    boolean testIfWord = false;


    try {
        InputStream inputStream = new FileInputStream(textFileLocation);
        byte byte1 = (byte) inputStream.read();
        while (byte1 != -1) {

            readByteCharByChar +=String.valueOf((char)byte1);
            for(int i=0;i<excludedSymbols.length;i++) {
            if(readByteCharByChar.equals(excludedSymbols[i])) {
                if(!readWords.equals("")) {
                extractOnlyWordsFromTextFile.add(readWords);
                }
                readWords ="";
                testIfWord = true;
                break;
            }
            }
            if(!testIfWord) {
                readWords+=(char)byte1;
            }
            readByteCharByChar = "";
            testIfWord = false;
            byte1 = (byte)inputStream.read();
            if(byte1 == -1 && !readWords.equals("")) {
                extractOnlyWordsFromTextFile.add(readWords);
            }
        }
        inputStream.close();
        System.out.println(extractOnlyWordsFromTextFile);
        System.out.println("The number of words in the choosen text file are: " + extractOnlyWordsFromTextFile.size());
    } catch (IOException ioException) {

        ioException.printStackTrace();
    }
}

这是我的ViewHolder:

from selenium import webdriver
import os
driver = webdriver.Chrome("C:\Python27\Scripts\chromedriver.exe")
#removes the file if it already exists
try:
    os.remove('E:\dhd.png')
except WindowsError: 
    #print("no file found")
    pass
# Maximize the browser window
driver.maximize_window()
driver.get("https://accounts.google.com/ServiceLogin/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1&flowName=GlifWebSignIn&flowEntry=AddSession")
driver.save_screenshot('e:\dhd.png')
driver.quit()

这是我传递给Firebase和适配器的Message对象(我输入的数据同步到Firebase没有任何问题,我对firebase的查询以正确的格式获取数据):

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


        RecyclerView mRecyclerView;
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        Query query = myMessagesRef.child(RoomID)
                .orderByKey()
                .limitToLast(50);

        FirebaseRecyclerOptions<Message> options =
                    new FirebaseRecyclerOptions.Builder<Message>()
                            .setQuery(query, Message.class)
                            .build();

        FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Message, ChatHolder>(options) {


            @Override
            public ChatHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                // Create a new instance of the ViewHolder, in this case we are using a custom
                // layout called R.layout.message for each item
                final View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.recycler_item, parent, false);

                return new ChatHolder(view);
            }

            @Override
            protected void onBindViewHolder(ChatHolder holder, int position, Message model) {
                //super.onBindViewHolder(holder, position);
                Log.w(TAG, "Some Info on messages 2 " + model.MessageText);
                holder.bindChat(model);

            }



        };
        mRecyclerView.setAdapter(adapter);

    }

这是我的活动布局XML(名称:activity_message_details.xml): (注意底部附近的回收者视图):

static class ChatHolder extends RecyclerView.ViewHolder{
    TextView mtext;
    //Context mContext;


    public ChatHolder(View v) {
        super(v);
        mtext = (TextView) v.findViewById(com.example.administrationuser.piclo.R.id.textView13);
    }

    public void bindChat(Message mess){
        mtext.setText(mess.MessageText);
    }


}

最后,这是我的View布局XML(名称:recycler_item.xml):

public static class Message {

    public String UserID;
    public String UserName;
    public String MessageText;

    public Message() {}  // Needed for Firebase

    public Message(String UserID, String UserName, String MessageText) {
        this.UserID = UserID;
        this.UserName = UserName;
        this.MessageText = MessageText;
    }
}

2 个答案:

答案 0 :(得分:3)

有两件事需要解决。

1)添加adapter.startListening();(如Elvin No Matter所述)。在我的情况下,我在onCreate末尾附近的mRecyclerView.setAdapter(adapter);下面添加了这个。

2)将我的View Layout XML封装在<android.support.v7.widget.LinearLayoutCompat>中。查看我的新View Holder,以及其他一些不相关的按钮更改:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat
    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="wrap_content"
    android:layout_height="wrap_content"
 >

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp">

        <TextView
            android:id="@+id/textView13"
            android:layout_width="285dp"
            android:layout_height="24dp"
            android:text="TextView"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            tools:layout_editor_absoluteX="-6dp" />

        <TextView
            android:id="@+id/textView14"
            android:layout_width="123dp"
            android:layout_height="24dp"
            android:layout_alignParentTop="true"
            android:layout_marginRight="8dp"
            android:text="TextView"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="@+id/textView13"
            app:layout_constraintTop_toTopOf="parent"
            tools:layout_editor_absoluteX="154dp" />

    </android.support.constraint.ConstraintLayout>

</android.support.v7.widget.LinearLayoutCompat>

答案 1 :(得分:1)

根据readme

  

FirestoreRecyclerAdapter使用快照侦听器进行监视   更改Firestore查询。要开始收听数据,请致电   startListening()方法。您可能想在onStart()中调用它   方法。确保您已完成所需的任何身份验证   在调用startListening()之前读取数据,否则您的查询将失败。

Process

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