简体   繁体   English

如何创建可变的 Firebase 数据库名称?

[英]How do i create a variable firebase database name?

I want to search a variable data using the Firebase database.我想使用 Firebase 数据库搜索变量数据。

My problem.我的问题。 I'm using an edit text.我正在使用编辑文本。 I want to show the value of the database I wrote here as a text.我想将我在这里写的数据库的值显示为文本。 I only managed to do that.我只能做到这一点。 But under the data in my database, there is a data named "name" that works.但是在我的数据库中的数据下,有一个名为“name”的数据有效。

-L3131131313 112: "ambulans" name: "ambulans" -L3131131313 112:“救护车”名称:“救护车”

When I call "112" with Edit Text, it gives me an "ambulance".当我用编辑文本呼叫“112”时,它给了我一辆“救护车”。 But I want to do it when there's no database named "name."但是当没有名为“name”的数据库时,我想这样做。

This is what should be.这是应该的。 The value of "112" should be "ambulance." “112”的值应该是“救护车”。

-L3131131313 112: "ambulans" -L3131131313 112:“救护车”

The value "112" will be continuously variable.值“112”将是连续可变的。 For example, "110", "200", "352", etc. I would like to see the value opposite to the data I'm querying with Edit Text, but I failed.例如,“110”、“200”、“352”等。我想看到与我使用“编辑文本”查询的数据相反的值,但我失败了。 I'd appreciate it if you could help.如果您能提供帮助,我将不胜感激。 Sorry for my bad english.对不起,我的英语不好。

My database photo;我的数据库照片; 在此处输入图片说明

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);
}

My Category Item我的类别项目

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;
}

}

UPDATE更新

I've solved some of my problems.我已经解决了我的一些问题。 The sample database I will use is as follows.我将使用的示例数据库如下。 Now I want to do, but I can not produce a solution.现在我想做,但我无法产生解决方案。

There will be two or more sub-database sets in our database.我们的数据库中将有两个或多个子数据库集。 For example, I will use this database name "+90 505 696 1234" twice.例如,我将使用这个数据库名称“+90 505 696 1234”两次。 The value of this database is "A" and the value of "AA" in the other set.这个数据库的值为“A”,另一个集合中的值为“AA”。 When I call this phone number with Edit Text, I want it to give me the value "A" and "AA".当我使用 Edit Text 拨打这个电话号码时,我希望它给我值“A”和“AA”。 But I can't make it.但我做不到。 Because the CategoryItem class I created is not a variable.因为我创建的 CategoryItem 类不是变量。 I used the name as String value in CategoryItem.我在 CategoryItem 中使用名称作为字符串值。 I call it with getName ().我用 getName() 调用它。 But if this value is fixed, it works.但是如果这个值是固定的,它就可以工作。 I cannot find a solution because the user will search for different database names with the help of edit text.我找不到解决方案,因为用户将在编辑文本的帮助下搜索不同的数据库名称。 I'il be very happy if you can help.如果你能帮忙我会很高兴的

My Firebase Database我的 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"
    }
  }
}

My CategoryItem.java我的 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;
    }

}

I want to search a variable data using the Firebase database.我想使用 Firebase 数据库搜索变量数据。

My problem.我的问题。 I'm using an edit text.我正在使用编辑文本。 I want to show the value of the database I wrote here as a text.我想将我在这里写的数据库的值显示为文本。 I only managed to do that.我只能做到这一点。 But under the data in my database, there is a data named "name" that works.但是在我的数据库中的数据下,有一个名为“name”的数据有效。

-L3131131313 112: "ambulans" name: "ambulans" -L3131131313 112:“救护车”名称:“救护车”

When I call "112" with Edit Text, it gives me an "ambulance".当我用编辑文本呼叫“112”时,它给了我一辆“救护车”。 But I want to do it when there's no database named "name."但是当没有名为“name”的数据库时,我想这样做。

This is what should be.这是应该的。 The value of "112" should be "ambulance." “112”的值应该是“救护车”。

-L3131131313 112: "ambulans" -L3131131313 112:“救护车”

The value "112" will be continuously variable.值“112”将是连续可变的。 For example, "110", "200", "352", etc. I would like to see the value opposite to the data I'm querying with Edit Text, but I failed.例如,“110”、“200”、“352”等。我想看到与我使用“编辑文本”查询的数据相反的值,但我失败了。 I'd appreciate it if you could help.如果您能提供帮助,我将不胜感激。 Sorry for my bad english.对不起,我的英语不好。

My database photo;我的数据库照片; 在此处输入图片说明

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);
}

My Category Item我的类别项目

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;
}

}

UPDATE更新

I've solved some of my problems.我已经解决了我的一些问题。 The sample database I will use is as follows.我将使用的示例数据库如下。 Now I want to do, but I can not produce a solution.现在我想做,但我无法产生解决方案。

There will be two or more sub-database sets in our database.我们的数据库中将有两个或多个子数据库集。 For example, I will use this database name "+90 505 696 1234" twice.例如,我将使用这个数据库名称“+90 505 696 1234”两次。 The value of this database is "A" and the value of "AA" in the other set.这个数据库的值为“A”,另一个集合中的值为“AA”。 When I call this phone number with Edit Text, I want it to give me the value "A" and "AA".当我使用 Edit Text 拨打这个电话号码时,我希望它给我值“A”和“AA”。 But I can't make it.但我做不到。 Because the CategoryItem class I created is not a variable.因为我创建的 CategoryItem 类不是变量。 I used the name as String value in CategoryItem.我在 CategoryItem 中使用名称作为字符串值。 I call it with getName ().我用 getName() 调用它。 But if this value is fixed, it works.但是如果这个值是固定的,它就可以工作。 I cannot find a solution because the user will search for different database names with the help of edit text.我找不到解决方案,因为用户将在编辑文本的帮助下搜索不同的数据库名称。 I'il be very happy if you can help.如果你能帮忙我会很高兴的

My Firebase Database我的 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"
    }
  }
}

My CategoryItem.java我的 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM