如何从Firebase实时数据库中获取数据并将其显示在微调器上

时间:2020-06-23 16:51:26

标签: java android firebase firebase-realtime-database

您好,我已将值保存在Firebase实时数据库中。但是我现在想获取它并在微调器中显示它。 看到数据库firebaseData的这张照片 有两个名为Jokes和Shayari的数据库...它们中有子类别。我想每当用户在textview中输入Jokes时,Jokes数据库的所有子类别都应显示在微调器中,而当他输入Shayari时,所有Shayari的所有子类别都应显示在微调器中。 请告诉我如何根据条件获取并在微调框中显示。

我在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();
    }
}

这是我的ModelClass代码:

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

}

请告诉我们如何获取它们。

1 个答案:

答案 0 :(得分:0)

首先,告诉用户添加了子类别的方式需要改进。

代替:

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

尝试使用:

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

通过这种方式,您可以确保成功添加数据后,消息仅显示

第二,为了显示微调器中DB的数据,我创建了一个简单的函数:

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

然后称呼它:

setSpinnerDataFromDB(mRef, this, mSpinner);

希望它对您有帮助:)

相关问题