简体   繁体   中英

How to fetch data from firebase realtime database and display it on spinner

Hello i have saved values in firebase realtime database. but i now i want to fetch it and display it in spinner. see this photo of database firebaseData there are two database named Jokes and Shayari...and they have subcategory in it. I want whenever user enter Jokes in textview then all subcategory of Jokes database should display in spinner and when he enters Shayari then all Shayari's subcategory should display in spinner. Please tell me how can i fetch it on the basis of condition and show them in spinner.

My code of storing the data in firebase.

 private void addSubCategory() {
    String subcategory = editText.getText().toString().trim();
    String parentcategory = spinner.getSelectedItem().toString();
    if(parentcategory.equals("Jokes")){
        String id = databaseJokes.push().getKey();
        SubModel subModel= new SubModel(id, parentcategory, subcategory);
        databaseJokes.child(id).setValue(subModel);
        Toast.makeText(this, "Jokes Subcategory added", Toast.LENGTH_SHORT).show();
        editText.getText().clear();
    }
    else if(parentcategory.equals("Shayari")){
        String id = databaseJokes.push().getKey();
        SubModel subModel= new SubModel(id, parentcategory, subcategory);
        databaseShayari.child(id).setValue(subModel);
        Toast.makeText(this, "Shayari Subcategory added", Toast.LENGTH_SHORT).show();
        editText.getText().clear();
    }
    else if(!TextUtils.isEmpty(subcategory)){
        Toast.makeText(this, "Please enter Sub Category", Toast.LENGTH_SHORT).show();
    }
}

It is my ModelClass code:

public class SubModel {
String categoryId;
String parentcategory;
String subcategory;
public SubModel(){

}

public SubModel(String categoryId, String parentcategory, String subcategory) {
    this.categoryId = categoryId;
    this.parentcategory = parentcategory;
    this.subcategory = subcategory;
}

public String getCategoryId() {
    return categoryId;
}

public String getParentcategory() {
    return parentcategory;
}

public String getSubcategory() {
    return subcategory;
}

}

Please tell how to fetch them.

First of all, the way which tell the user that their subcategory was added need some improvement.

instead of:

databaseShayari.child(id).setValue(subModel);
Toast.makeText(this, "Shayari Subcategory added", Toast.LENGTH_SHORT).show();

try to use:

databaseShayari.child(id).setValue(subModel).addOnCompleteListener(task -> {
    if (task.isSuccessful()) {
        Toast.makeText(this, "Shayari Subcategory added", Toast.LENGTH_SHORT).show();
       }
    });

in this way you ensure the message will show only when the data added successfully.

Second, to display data from DB in your spinner, I create a simple function do this:

public void setSpinnerDataFromDB(DatabaseReference mRef, final Context context, final Spinner spinner) {
    mRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            final List<String> listCategoryId = new ArrayList<String>();
            for (DataSnapshot snap : dataSnapshot.getChildren()) {
                String categoryId = snap.child(context.getString(R.string.depart_name_fire_emp)).getValue(String.class);

                listCategoryId.add(categoryId);
            }

            ArrayAdapter<String> departsAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, listCategoryId);
            departsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(departsAdapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}

then call it:

setSpinnerDataFromDB(mRef, this, mSpinner);

Hope it helps you:)

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