从Firebase数据库读取父键的所有子键

时间:2019-04-21 18:27:02

标签: android firebase

我最近将我的应用连接到了Firebase,我需要一些帮助。 我想显示图片中的类别,但不知道如何完成。

https://imgur.com/a/v812Roc

更新: 我将类别添加到了父键

https://imgur.com/a/epaszdO

我将以下代码用于Firebase:

更新:新的无效的readDataCategory

 FirebaseDatabase firebaseDatabase;
    Context context;

    DatabaseReference reference;

    public OnlineDBHelper(Context context , FirebaseDatabase firebaseDatabase) {
        this.firebaseDatabase = firebaseDatabase;
        this.context = context;
        reference=this.firebaseDatabase.getReference("AllInOne");
    }

    private static OnlineDBHelper instance;

    public static synchronized OnlineDBHelper getInstance(Context context, FirebaseDatabase firebaseDatabase){
        if (instance==null)
            instance=new OnlineDBHelper(context,firebaseDatabase);
        return instance;
    }


    public void readData(final MyCallback myCallback , String category) {
        final AlertDialog dialog = new SpotsDialog.Builder()
                .setContext(context)
                .setCancelable(false)
                .build();

        if (!dialog.isShowing())
            dialog.show();

        reference.child(category)
                .child("question")
                .addListenerForSingleValueEvent(new ValueEventListener() {


                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        List<Question> questionList = new ArrayList<>();


                        //////// Questions ///////

                        for (DataSnapshot questionSnapShot : dataSnapshot.getChildren())
                            questionList.add(questionSnapShot.getValue(Question.class));
                        myCallback.setQuestionList(questionList);

                        if (dialog.isShowing())
                            dialog.dismiss();
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {
                        Toast.makeText(context, "" + databaseError.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                });

         public void readDataCategory(final MiCategory mycategory,String category){
        final AlertDialog dialog =new SpotsDialog.Builder()
                .setContext(context)
                .setCancelable(false)
                .build();

        if (!dialog.isShowing())
            dialog.show();

        reference.child(category)
                .child("category")
                .addListenerForSingleValueEvent(new ValueEventListener() {


                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        List<Category> categoryList=new ArrayList<>();


                        //////// MiCategory ////////
                        for (DataSnapshot categorySnapShot:dataSnapshot.getChildren())
                            categoryList.add(categorySnapShot.getValue(Category.class));
                               mycategory.setCategoryList(categoryList);


                        if (dialog.isShowing())
                            dialog.dismiss();
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {
                        Toast.makeText(context,""+databaseError.getMessage(),Toast.LENGTH_SHORT).show();
                    }

                });

    }

我拥有MyCallback接口

public interface MyCallback {
    void setQuestionList(List<Question>questionList);

}

更新:新界面MiCategory

public interface MiCategory {

    void setCategoryList(List<Category> categoryList);
}

在应该显示类别的活动中,我将这一行更改为firebasedatabase(当前,我在本地数据库中具有类别,但是我想在实时数据库中更改)。

更新: 我在本地数据库的这一行中添加了注释,并添加了类似于firebase的内容,但是我在OnlineDBHelper中出现错误“ readDataCategory(MiCategory,String),无法将其附加到MiCategory”。

    //CategoryAdapter adapter=new CategoryAdapter(Quiz.this,  DBHelper.getInstance(this).getAllCategories());
        CategoryAdapter adapter=new CategoryAdapter(Quiz.this,  OnlineDBHelper.getInstance(this,
                FirebaseDatabase.getInstance())
                .readDataCategory(new MiCategory() {
                    @Override
                    public void setCategoryList(List<Category> categoryList) {
                       Common.categoryList.clear();
                       Common.categoryList=categoryList;
                        if (Common.categoryList.size() == 0) {
                            new MaterialStyledDialog.Builder(Quiz.this)
                                    .setTitle("Oppss")
                                    .setDescription("We don't have any " + Common.selectedCategory.getName() + " Category")
                                    .setPositiveText("ok")
                                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                                        @Override
                                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                            dialog.dismiss();
                                            finish();
                                        }
                                    }).show();

                        }
                    }
                }));

0 个答案:

没有答案