FirestoreAdapter无法正常填充回收站视图 [英] FirestoreAdapter not working to populate a Recycler View

查看:62
本文介绍了FirestoreAdapter无法正常填充回收站视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android的新秀,因此决定将Cloud Firestore用作我的数据库,并使用RecyclerView填充MainActivity中的列表.我以为在Firestore中使用Firebase-UI会超级简单,但是我不知道为什么RecyclerView中什么也没装.作为参考,这是Firebase UI页面这是我基于我的应用程序创建的示例代码.我要做的就是填充用户已保存在数据库中的课程"列表(Course.java如下,因为它只是一个带有名称,日期等的简单POJO,因此未在下面显示).

I'm a rookie with Android and decided to use Cloud Firestore as my db and a RecyclerView for populating a list in my MainActivity. I thought using Firebase-UI for Firestore would be super easy, but I can't figure out why nothing is populating in my RecyclerView. For reference, here is the Firebase UI page and here is the example code that I based my app off of. All I'm trying to do is populate a list of "Courses" (Course.java not displayed below since it's just a simple POJO with name, date, etc.) the user has saved in the db.

MainActivity:

MainActivity:

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.Query;
import com.lorin.deadlineschmedline.R;
import com.lorin.deadlineschmedline.adapters.CourseAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity
        implements CourseAdapter.OnCourseSelectedListener,
        PopupMenu.OnMenuItemClickListener {

private FirebaseFirestore mFirestore;
private Query mQuery;
private FirebaseUser mCurrentUser;

private RecyclerView.Adapter mAdapter;

private static final String TAG = "MainActivity";
private static final int RC_SIGN_IN = 9001;
private static final int LIMIT = 50;

@BindView(R.id.rv_course_item_list)
RecyclerView mCourseRecyvlerView;

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

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showPopup(view);
        }
    });

    // Enable Firestore logging
    FirebaseFirestore.setLoggingEnabled(true);

    // Firestore
    mFirestore = FirebaseFirestore.getInstance();
    mQuery = mFirestore.collection("courses").whereEqualTo("name", true).limit(LIMIT);
    mAdapter = new CourseAdapter(mQuery, this) {
        @Override
        protected void onDataChanged() {
            // Show/hide content if the query returns empty.
            if (getItemCount() == 0) {
                mCourseRecyvlerView.setVisibility(View.VISIBLE);
                //mEmptyView.setVisibility(View.VISIBLE);
            } else {
                mCourseRecyvlerView.setVisibility(View.VISIBLE);
                //mEmptyView.setVisibility(View.GONE);
            }
        }

        @Override
        protected void onError(FirebaseFirestoreException e) {
            // Show a snackbar on errors
            Snackbar.make(findViewById(android.R.id.content),
                    "Error: check logs for info.", Snackbar.LENGTH_LONG).show();
        }
    };

    mCourseRecyvlerView.setLayoutManager(new LinearLayoutManager(this));
    mCourseRecyvlerView.setAdapter(mAdapter);
}

private void showPopup(View v) {
    PopupMenu popup = new PopupMenu(this, v);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.add_component_menu, popup.getMenu());
    popup.setOnMenuItemClickListener(this);
    popup.show();
}

@Override
public void onStart() {
    super.onStart();
    mAdapter.startListening();
}

@Override
public void onStop() {
    super.onStop();

    if (mAdapter != null) {
        mAdapter.stopListening();
    }
}

@Override
public boolean onMenuItemClick(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.add_course:
            startActivity(new Intent(MainActivity.this, AddCourseActivity.class));
            return true;
        case R.id.add_course_component:
            startActivity(new Intent(MainActivity.this, AddCourseComponentActivity.class));
            return true;
        default:
            return true;
    }
}

@Override
public void onCourseSelected(DocumentSnapshot restaurant) {
    // Go to the details page for the selected activity
    Intent intent = new Intent(this, AddCourseActivity.class);
    startActivity(intent);

    //overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_to_left);
}

}

我尝试使用调试器逐步遍历MainActivity,但是查询数据库时,我无法从Firestore中获得任何收益(可能是问题).任何想法和反馈将不胜感激!

I've tried stepping through the MainActivity with the debugger, but I can't make heads or tails of what I'm getting back from Firestore when I Query the db (which may be the issue). Any thoughts and feedback would be greatly appreciated!

推荐答案

原来是Firestore查询存在问题.以前,我曾经使用过:

It turned out to be an issue with the Firestore Query. Previously, I had used:

mQuery = mFirestore.collection("courses").whereEqualTo("name", true).limit(LIMIT);

但是,似乎在进行Firestore查询时必须使用orderBy()方法.使用以下方法可以达到目的:

However, it appears that you must use the orderBy() method when making a Firestore Query. Using the following did the trick:

mQuery = mFirestore.collection("courses").orderBy("name").limit(LIMIT);

现在我的数据已按预期显示在RecyclerView中.

Now my data is showing up in the RecyclerView as anticipated.

这篇关于FirestoreAdapter无法正常填充回收站视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆