Android Studio和Firebase数据库问题

时间:2018-12-06 10:55:18

标签: java android firebase firebase-realtime-database

我的数据库看起来像这样

enter image description here

我的名字是Anand,我创建了一个应用程序,并使用了底部导航。使用了3个片段,其中一个片段用于显示课程(pdf文件)。我只想向特定用户显示特定课程,这意味着用户注册后,他们将强制选择课程。根据他们选择的课程名称,他们只需要获取特定的pdf文件。根据我当前的代码,它可以显示所有内容,而我只需要根据下面提到的课程名称和用户课程.my代码来显示

在此处输入代码

public class CourseFragment extends android.support.v4.app.Fragment {
//the listview
ListView listView;

//database reference to get uploads data
DatabaseReference mDatabaseReference;

//list to store uploads data
List<Upload> uploadList;

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_course_fragment, null);

    uploadList = new ArrayList<>();
    listView = (ListView) v. findViewById(R.id.listView);
    final  WebView webView= (WebView) v. findViewById(R.id.webview);

       //adding a clicklistener on listview
       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
               //getting the upload
               Upload upload = uploadList.get(i);

               //Opening the upload file in browser using the upload url
               Intent intent = new Intent(Intent.ACTION_VIEW);
               intent.setData(Uri.parse(upload.getUrl()));
               startActivity(intent);

           }
       });


    //getting the database reference
    mDatabaseReference = FirebaseDatabase.getInstance().getReference(Constants.DATABASE_PATH_UPLOADS);

    //retrieving upload data from firebase database
    mDatabaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                Upload upload = postSnapshot.getValue(Upload.class);
                uploadList.add(upload);
            }

            String[] uploads = new String[uploadList.size()];

            for (int i = 0; i < uploads.length; i++) {
                uploads[i] = uploadList.get(i).getName();
            }

            //displaying it to list
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, uploads);
            listView.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    return v;
}

1 个答案:

答案 0 :(得分:0)

要解决此问题,请使用以下代码行:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query usersQuery = rootRef.child("Users").orderByChild("name").equalTo("ANAND");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String course = ds.child("course").getValue(String.class);
            Log.d(TAG, course);

            Query courseRef = rootRef.child("Course").orderByChild("name").equalTo(course);
            ValueEventListener eventListener = new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for(DataSnapshot ds : dataSnapshot.getChildren()) {
                        String name = ds.child("name").getValue(String.class);
                        String url = ds.child("url").getValue(String.class);
                        Log.d(TAG, name + " / " + url);
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
                }
            };
            courseRef.addListenerForSingleValueEvent(eventListener);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
usersQuery.addListenerForSingleValueEvent(valueEventListener);

该查询背后的逻辑如下,首先我们查询数据库以找到名称为ANAND的用户,然后获取相应的课程名称。有了课程名称,我们可以查询数据库以获得相应课程的相应URL /名称。因此,在这种情况下,logcat中的输出将为:

B / https://firebasestorage.googleapis.com...