Error: Can't call startAt() or equalTo() multiple times
Error: Can't call startAt() or equalTo() multiple times
我想在 Firebase 数据库中查找并列出用户使用 EditText 编写的文本。
由于我的应用中会用到很多数据,所以我分页列出来。我正在使用图书馆列出。
这是我的问题。当我想将查询要查找的数据限制为 startAt ()
和 endAt ()
时,应用程序崩溃并出现此错误。
"Can't call startAt() or equalTo() multiple times"
当我从查询中删除 startAt ()
和 endAt ()
时,没有问题,但是列出了所有数据而不是用户请求的数据。我认为查询 startAt()
和 endAt()
不适用于我使用的库。但我必须使用它。有了这个库,我可以查询用户用 EditText 编写的文本的 Firebase 数据库。
注意:应用程序失败但在出错之前找到并列出了用户正在查找的数据。
抱歉我的英语不好。
My 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'
}
My User.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;
}
}
My 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);
}
}
My Activity:
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();
}
}
FirebaseUI FirebaseRecyclerPagingAdapter
使用 Firebase 实时数据库查询对数据进行分页。因此,它必须与 DatabaseReference
或 Query
一起无限制地使用,并且不能与您自己的 query/limits.
组合使用
Firebase 数据库查询只能在单个 属性 上 order/filter。在许多情况下,可以将要过滤的值组合成一个(合成的)属性。有关此方法和其他方法的示例,请在此处查看我的回答:。但是如果你走这条路,很可能你必须在数据结构上实现你自己的适配器。
我想在 Firebase 数据库中查找并列出用户使用 EditText 编写的文本。
由于我的应用中会用到很多数据,所以我分页列出来。我正在使用图书馆列出。
这是我的问题。当我想将查询要查找的数据限制为 startAt ()
和 endAt ()
时,应用程序崩溃并出现此错误。
"Can't call startAt() or equalTo() multiple times"
当我从查询中删除 startAt ()
和 endAt ()
时,没有问题,但是列出了所有数据而不是用户请求的数据。我认为查询 startAt()
和 endAt()
不适用于我使用的库。但我必须使用它。有了这个库,我可以查询用户用 EditText 编写的文本的 Firebase 数据库。
注意:应用程序失败但在出错之前找到并列出了用户正在查找的数据。
抱歉我的英语不好。
My 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'
}
My User.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;
}
}
My 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);
}
}
My Activity:
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();
}
}
FirebaseUI FirebaseRecyclerPagingAdapter
使用 Firebase 实时数据库查询对数据进行分页。因此,它必须与 DatabaseReference
或 Query
一起无限制地使用,并且不能与您自己的 query/limits.
Firebase 数据库查询只能在单个 属性 上 order/filter。在许多情况下,可以将要过滤的值组合成一个(合成的)属性。有关此方法和其他方法的示例,请在此处查看我的回答:。但是如果你走这条路,很可能你必须在数据结构上实现你自己的适配器。