小编典典

FirestoreAdapter无法正常填充回收站视图

java

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

主要活动:

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中获得任何收益(可能是问题)。任何想法和反馈将不胜感激!


阅读 214

收藏
2020-12-03

共1个答案

小编典典

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

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

但是,似乎在进行Firestore查询时必须使用orderBy()方法。使用下面的技巧:

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

现在,我的数据按预期显示在RecyclerView中。

2020-12-03