现在我实现了搜索视图。此搜索视图按名称搜索梦想。但不知道如何在Firebase上实现此功能。
//这是我对Firebase的引用
database = FirebaseDatabase.getInstance();
myRef = database.getReference("dream");
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<Dream> dreamList = new ArrayList<>();
for (DataSnapshot dreamCheckedSnapshot : dataSnapshot.getChildren()) {
Dream dreamChecked = dreamCheckedSnapshot.getValue(Dream.class);
dreamList.add(dreamChecked);
}
dreamNotLoginAdapter.updateList(dreamList);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), "" + databaseError, Toast.LENGTH_SHORT).show();
}
});
//此方法显示搜索视图方法
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
}
答案 0 :(得分:0)
尝试这个
Query query = firebaseReference.orderByChild(CONSTANT.KEY_USER_NAME).startAt(stringQuery).limitToFirst(20);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
Popular mTrading = dataSnapshot1.getValue(Popular.class);
mTradingArrayList.add(mTrading);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
UTILS.showAlertDialog(getActivity(), getString(R.string.alertUnexpectedError), getString(R.string.app_name), getString(R.string.ok));
}
});