我想使用Firebase数据库搜索变量数据。
我的问题。我正在使用编辑文字。我想以文字形式显示我在此处编写的数据库的价值。我只是做到了。但是在我数据库中的数据下,有一个名为“ name”的数据可以正常工作。
-L3131131313 112: “雨伞” 名称: “雨伞”
当我用“编辑文本”呼叫“ 112”时,它给了我“救护车”。但是,当没有名为“ name”的数据库时,我想这样做。
这应该是。值“ 112”应为“救护车”。
-L3131131313 112: “雨伞”
值“ 112”将是连续可变的。例如,“ 110”,“ 200”,“ 352”等。我想查看与我使用“编辑文本”查询的数据相反的值,但失败了。如果您能提供帮助,我将不胜感激。对不起,我的英语不好。
我的数据库照片; [https://i.hizliresim.com/jgor3W.png][1]
public class MainActivity extends AppCompatActivity {
EditText Search_Edit_Text;
Button Search_Button;
RecyclerView Search_Contact_List;
DatabaseReference mUserDatabase;
FirebaseRecyclerOptions<CategoryItem> options,options2;
FirebaseRecyclerAdapter<CategoryItem,CategoryViewHolder> adapter;
Query firebaseSearchQuery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("database");
Search_Edit_Text = (EditText) findViewById(R.id.Search_Edit_Text);
Search_Button = (Button) findViewById(R.id.Search_Button);
Search_Contact_List = (RecyclerView) findViewById(R.id.Search_Contact_List);
Search_Contact_List.setHasFixedSize(true);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getBaseContext(),2);
Search_Contact_List.setLayoutManager(gridLayoutManager);
Search_Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String searchText = Search_Edit_Text.getText().toString();
firebaseUserSearch(searchText);
}
});
private void firebaseUserSearch(String searchText) {
Toast.makeText(MainActivity.this, "Started Search",
Toast.LENGTH_LONG).show();
firebaseSearchQuery = mUserDatabase.orderByChild(searchText).startAt("").endAt("" + "\uf8ff");
options2 = new FirebaseRecyclerOptions.Builder<CategoryItem>()
.setQuery(firebaseSearchQuery,CategoryItem.class)
.build();
adapter = new FirebaseRecyclerAdapter<CategoryItem,
CategoryViewHolder>(options2) {
@Override
protected void onBindViewHolder(@NonNull final CategoryViewHolder holder, int position, @NonNull final CategoryItem model) {
}
@NonNull
@Override
public CategoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_layout,parent,false);
return new CategoryViewHolder(itemView);
}
};
setCategory();
}
private void setCategory() {
adapter.startListening();
Search_Contact_List.setAdapter(adapter);
}
我的类别项目
public class CategoryItem {
public String name;
public CategoryItem() {
}
public CategoryItem(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
更新
我已经解决了一些问题。我将使用的示例数据库如下。现在我想做,但是我无法解决。
我们的数据库中将有两个或多个子数据库集。例如,我将两次使用此数据库名称“ +90 505 696 1234”。该数据库的值是“ A”,其他集中的值是“ AA”。当我用“编辑文本”呼叫该电话号码时,我希望它给我值“ A”和“ AA”。但是我做不到。因为我创建的CategoryItem类不是变量。我在CategoryItem中将名称用作String值。我用getName()来称呼它。但是,如果此值是固定的,则可以使用。我找不到解决方案,因为用户将在编辑文本的帮助下搜索不同的数据库名称。如果可以的话,我会很高兴。
我的Firebase数据库
{
"ContactPhoneNumbers" : {
"-LcaHYcsoGA-VT8yvgGf" : {
"+90 505 696 1234" : "A",
"+90 506 854 2345" : "B",
"+90 530 408 3456" : "C",
"+90 535 966 4567" : "D",
"+90 536 782 5678" : "E",
"+90 546 934 67 89" : "F",
"+905304080001" : "G",
"+905316910002" : "H",
"+905359660003" : "I",
"+905367820004" : "J",
"+905425420005" : "K",
"+905469340006" : "L",
"05056960007" : "M"
},
"-LcaH_gtgarJwbY5-C08" : {
"+90 505 696 1234" : "AA",
"+90 506 854 2345" : "BB",
"+90 530 408 3456" : "CAC",
"+90 535 966 4567" : "AAA",
"+90 536 782 5678" : "CAB",
"+90 546 934 67 89" : "BB",
"+905304080001" : "A",
"+905316910002" : "BBB",
"+905359660003" : "DDD",
"+905367820004" : "EEE",
"+905425420005" : "FFF",
"+905469340006" : "L",
"05056960007" : "M"
}
}
}
我的CategoryItem.java
public class CategoryItem {
public String name ;
public CategoryItem() {
}
public CategoryItem(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
答案 0 :(得分:2)
有很多方法可以解决您遇到的问题。但是解决方案因数据集的复杂性而异。
注意:在使用电话号码时,必须注意确保Firebase不会将其解释为数字,而是保留为字符串。对于普通电话号码,在号码开头添加"_"
就足够了。如果您希望将这些号码与带有国际呼叫者代码的号码区分开,可以用"_"
代替"+"
。
例如“ _5417543010”和“ +15417543010”是相同的美国编号。
如果仅将数字与字符串匹配(反之亦然),则可以使用以下设置:
{
"ContactPhoneNumbers": {
"_110": "fireman",
"_112": "ambulance",
"_155": "police",
"_ambulance": "_112",
"_fireman": "_110",
"_police": "_155"
}
}
private Query firebaseSearchQueryContacts(String searchText) {
// will return all entries that start with value of "searchText"
return mUserDatabase.child("ContactPhoneNumbers").startAt("_" + searchText).endAt("_" + searchText + "\uf8ff");
}
要将姓名或电话号码与联系人匹配,可以使用以下设置:
{
"ContactPhoneNumbers": {
"L213213213232321321": {
"name": "police",
"phone": "_155"
},
"L312312312": {
"name": "fireman",
"phone": "_110"
},
"L3131131313": {
"name": "ambulance",
"phone": "_112"
}
}
}
private Query firebaseSearchQueryContactsByPhone(String phone) {
// will return all entries that have a phone number that starts with value of "phone"
return mUserDatabase.child("ContactPhoneNumbers").orderByChild("phone").startAt("_" + phone).endAt("_" + phone + "\uf8ff");
}
private Query firebaseSearchQueryContactsByName(String name) {
// will return all entries that have a name that starts with value of "name"
return mUserDatabase.child("ContactPhoneNumbers").orderByChild("name").startAt(name).endAt(name + "\uf8ff");
}
要将姓名或电话号码与联系人匹配,可以使用以下设置:
{
"ContactPhoneNumbers": {
"L213213213232321321": {
"name": "police",
"phone": "_155"
},
"L312312312": {
"name": "fireman",
"phone": "_110"
},
"L3131131313": {
"name": "ambulance",
"phone": "_112"
}
},
"ContactPhoneNumbersIndex"" {
"_110": "L312312312",
"_112": "L3131131313",
"_155": "L213213213232321321",
"_fireman": "L312312312",
"_ambulance": "L3131131313",
"_police": "L213213213232321321"
}
}
private Query firebaseSearchQueryContactEntriesByPhone(String phone) {
// will return all entries that have a phone number that starts with value of "phone"
return mUserDatabase.child("ContactPhoneNumbers").orderByChild("phone").startAt("_" + phone).endAt("_" + phone + "\uf8ff");
}
private Query firebaseSearchQueryContactEntriesByName(String name) {
// will return all entries that have a name that starts with value of "name"
return mUserDatabase.child("ContactPhoneNumbers").orderByChild("name").startAt(name).endAt(name + "\uf8ff");
}
private Query firebaseSearchQueryContactID(String searchText) {
// will return all IDs that have a phone/name value that starts with value of "searchText"
return mUserDatabase.child("ContactPhoneNumbersIndex").startAt("_" + searchText).endAt("_" + searchText + "\uf8ff");
}