[英]How do i create a variable firebase database name?
我想使用 Firebase 数据库搜索变量数据。
我的问题。 我正在使用编辑文本。 我想将我在这里写的数据库的值显示为文本。 我只能做到这一点。 但是在我的数据库中的数据下,有一个名为“name”的数据有效。
-L3131131313 112:“救护车”名称:“救护车”
当我用编辑文本呼叫“112”时,它给了我一辆“救护车”。 但是当没有名为“name”的数据库时,我想这样做。
这是应该的。 “112”的值应该是“救护车”。
-L3131131313 112:“救护车”
值“112”将是连续可变的。 例如,“110”、“200”、“352”等。我想看到与我使用“编辑文本”查询的数据相反的值,但我失败了。 如果您能提供帮助,我将不胜感激。 对不起,我的英语不好。
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”。 当我使用 Edit Text 拨打这个电话号码时,我希望它给我值“A”和“AA”。 但我做不到。 因为我创建的 CategoryItem 类不是变量。 我在 CategoryItem 中使用名称作为字符串值。 我用 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;
}
}
我想使用 Firebase 数据库搜索变量数据。
我的问题。 我正在使用编辑文本。 我想将我在这里写的数据库的值显示为文本。 我只能做到这一点。 但是在我的数据库中的数据下,有一个名为“name”的数据有效。
-L3131131313 112:“救护车”名称:“救护车”
当我用编辑文本呼叫“112”时,它给了我一辆“救护车”。 但是当没有名为“name”的数据库时,我想这样做。
这是应该的。 “112”的值应该是“救护车”。
-L3131131313 112:“救护车”
值“112”将是连续可变的。 例如,“110”、“200”、“352”等。我想看到与我使用“编辑文本”查询的数据相反的值,但我失败了。 如果您能提供帮助,我将不胜感激。 对不起,我的英语不好。
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”。 当我使用 Edit Text 拨打这个电话号码时,我希望它给我值“A”和“AA”。 但我做不到。 因为我创建的 CategoryItem 类不是变量。 我在 CategoryItem 中使用名称作为字符串值。 我用 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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.