【问题标题】:Error: Can't call startAt() or equalTo() multiple times错误:无法多次调用 startAt() 或 equalTo()
【发布时间】:2019-08-25 15:21:19
【问题描述】:

我想在 Firebase 数据库中查找并列出用户使用 EditText 编写的文本。

由于我将在我的应用程序中使用大量数据,因此我通过分页列出它们。我正在使用一个库来列出。

这是我的问题。当我想通过查询将要查找的数据限制为startAt ()endAt () 时,应用程序崩溃并出现此错误。

“不能多次调用 startAt() 或 equalTo()”

当我从查询中删除startAt ()endAt () 时,没有问题,但是列出了所有数据而不是用户请求的数据。我认为查询startAt()endAt() 不适用于我使用的库。但我必须使用它。使用这个库,我可以查询用户使用 EditText 编写的文本的 Firebase 数据库。

注意:应用程序失败,但会在出错之前找到并列出用户正在查找的数据。

对不起,我的英语不好。

我的 build.gradle:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.google.android.material:material:1.1.0-alpha08'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'

    implementation 'com.firebaseui:firebase-ui-database:4.3.2'
    implementation 'com.google.firebase:firebase-database:19.0.0'
    implementation 'com.google.android.gms:play-services-ads:18.1.1'

    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.nightonke:boommenu:2.1.1'
    implementation "com.andkulikov:transitionseverywhere:1.7.9"

    implementation "android.arch.paging:runtime:1.0.1" //For Paging Runtime
    implementation 'com.shreyaspatil:FirebaseRecyclerPagination:1.0.1' //For Firebase Recycler Paging

    testİmplementation 'junit:junit:4.12'
    androidTestİmplementation 'com.android.support.test:runner:1.0.2'
    androidTestİmplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

我的用户.java:

public class User {

    public String name;
    public String phone;

    public User() {

    }

    public User(String name, String phone) {
        this.name = name;
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

我的 NameViewHolder:

public class NameViewHolder extends RecyclerView.ViewHolder {

    public TextView result_name;

    public NameViewHolder(View itemView) {
        super(itemView);

        result_name = (TextView)itemView.findViewById(R.id.Search_Result_Name);
    }

}

我的活动:

public class MainActivity extends AppCompatActivity {

    EditText Search_Edit_Text;
    Button Search_Button;

    Query firebaseSearchQuery;
    DatabaseReference mUserDatabase;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    FirebaseRecyclerPagingAdapter<User, NameViewHolder> mAdapter;

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

        mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Contacts");

        mSwipeRefreshLayout = findViewById(R.id.swipe_refresh_layout);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mAdapter.refresh();
            }
        });

        Search_Edit_Text = (EditText) findViewById(R.id.Search_Edit_Text);
        Search_Button = (Button) findViewById(R.id.Search_Button);

        Search_Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

        String searchText = Search_Edit_Text.getText().toString().trim();

        firebaseUserSearchTest(searchText);
   }
}

private void firebaseUserSearchTest(String searchText) {

        Query myQuery = mUserDatabase.orderByChild("phone")
                .startAt(searchText).endAt(searchText + "\uf8ff"); //It works fine when I remove it, but the user doesn't have any data.

        PagedList.Config config = new PagedList.Config.Builder()
                .setEnablePlaceholders(false)
                .setPrefetchDistance(5)
                .setPageSize(10)
                .build();

        DatabasePagingOptions<User> optionss = new DatabasePagingOptions.Builder<User>()
                .setLifecycleOwner(this)
                .setQuery(myQuery, config, User.class)
                .build();

        mAdapter = new FirebaseRecyclerPagingAdapter<User, NameViewHolder>(optionss) {

            @NonNull
            @Override
            public NameViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new NameViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_layout, parent, false));
            }

            @Override
            protected void onBindViewHolder(@NonNull NameViewHolder holder, int position, @NonNull User model) {

                holder.result_name.setText(model.getName());
            }

            @Override
            protected void onLoadingStateChanged(@NonNull LoadingState state) {
                switch (state) {
                    case LOADING_INITIAL:
                    case LOADING_MORE:
                        // Do your loading animation
                        mSwipeRefreshLayout.setRefreshing(true);
                        break;

                    case LOADED:
                        // Stop Animation
                        mSwipeRefreshLayout.setRefreshing(false);
                        break;

                    case FINISHED:
                        //Reached end of Data set
                        mSwipeRefreshLayout.setRefreshing(false);
                        break;

                    case ERROR:
                        retry();
                        break;
                }
            }
        };

        Search_Contact_List.setAdapter(mAdapter);
    }

 @Override
    protected void onStart() {
        super.onStart();
        if (adapter !=null)
            adapter.startListening();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (adapter !=null)
            adapter.startListening();
        ShowInterstitial();
    }

    @Override
    protected void onStop() {
        if (adapter !=null)
            adapter.stopListening();
        super.onStop();
    }
}

【问题讨论】:

    标签: android firebase-realtime-database android-recyclerview firebaseui android-paging


    【解决方案1】:

    FirebaseUI FirebaseRecyclerPagingAdapter 使用 Firebase 实时数据库查询对数据进行分页。出于这个原因,它必须与DatabaseReferenceQuery 无限制地一起使用,并且不能与您自己的查询/限制结合使用。


    Firebase 数据库查询只能对单个属性进行排序/过滤。在许多情况下,可以将要过滤的值组合成一个(合成)属性。有关此方法和其他方法的示例,请在此处查看我的答案:http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase。但是如果你走这条路,很可能你必须在数据结构上实现你自己的适配器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-18
      • 1970-01-01
      • 2019-04-21
      相关资源
      最近更新 更多