简体   繁体   中英

How do i create a variable firebase database name?

I want to search a variable data using the Firebase database.

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.

-L3131131313 112: "ambulans" name: "ambulans"

When I call "112" with Edit Text, it gives me an "ambulance". But I want to do it when there's no database named "name."

This is what should be. The value of "112" should be "ambulance."

-L3131131313 112: "ambulans"

The value "112" will be continuously variable. 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. 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. The value of this database is "A" and the value of "AA" in the other set. When I call this phone number with Edit Text, I want it to give me the value "A" and "AA". But I can't make it. Because the CategoryItem class I created is not a variable. I used the name as String value in CategoryItem. I call it with 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

{
  "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

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.

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.

-L3131131313 112: "ambulans" name: "ambulans"

When I call "112" with Edit Text, it gives me an "ambulance". But I want to do it when there's no database named "name."

This is what should be. The value of "112" should be "ambulance."

-L3131131313 112: "ambulans"

The value "112" will be continuously variable. 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. 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. The value of this database is "A" and the value of "AA" in the other set. When I call this phone number with Edit Text, I want it to give me the value "A" and "AA". But I can't make it. Because the CategoryItem class I created is not a variable. I used the name as String value in CategoryItem. I call it with 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

{
  "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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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