android studio和firebase数据库问题

ecbunoof  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(364)

我的数据库是这样的

我的名字是anand,我创建了一个应用程序,我使用了底部导航。使用了3个片段,其中一个用于显示课程(pdf文件)。我只想向特定用户显示特定课程,这意味着当用户注册时,他们会强制选择课程。根据他们选择的课程名称,他们只能得到特定的pdf文件。根据我目前的代码,它显示了一切,我只需要显示根据课程名称和用户的课程。我的代码如下所述
在此处输入代码

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

wkyowqbh1#

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

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...

相关问题