我想在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'
}
我的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;
}
}
我的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();
}
}
答案 0 :(得分:0)
FirebaseUI FirebaseRecyclerPagingAdapter
使用Firebase实时数据库查询对数据进行分页。因此,它必须与无限制的DatabaseReference
或Query
一起使用,并且不能与您自己的查询/限制结合使用。
Firebase数据库查询只能对单个属性进行排序/过滤。在许多情况下,可以将要过滤的值组合到单个(合成)属性中。有关此方法和其他方法的示例,请在此处查看我的答案:http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase。但是,如果走这条路,很可能必须在数据结构上实现自己的适配器。