如何在另一个Java文件中匹配名称并显示Firebase中的详细信息?

时间:2018-11-12 04:12:37

标签: java android firebase firebase-realtime-database

假设我有 StaffAttendanceCheckIn.Java ,如果用户单击其中的 addOnItemTouchListener ,我想使其显示对话框或转到到另一页并显示出勤记录。例如,如果用户单击“事件名称Google”,它将显示Firebase记录。单击内部之后,它将显示出过滤条件。如果用户选择2018年11月6日星期二,它将显示入住日期和入住时间。我不确定 onDataChange

之后如何写

StaffAttendanceCheckIn.Java

package com.example.edward.neweventmanagementsystem;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.edward.neweventmanagementsystem.Model.EventInfo;
import com.example.edward.neweventmanagementsystem.ViewHolder.MenuViewHolder;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;

public class StaffAttendanceRecordTable extends AppCompatActivity {

    FirebaseDatabase database;
    DatabaseReference eventInfo;
    FloatingActionButton search_item;
    RecyclerView recycle_menu;
    RecyclerView.LayoutManager layoutManager;
    FirebaseRecyclerAdapter <EventInfo, MenuViewHolder> adapter;
    private DatabaseReference mDatabaseReference;
    private TextView EventName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_staff_attendance_record_table);

        recycle_menu = (RecyclerView) findViewById(R.id.recycle_menu);
        recycle_menu.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        recycle_menu.setLayoutManager(layoutManager);

        mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("AttendanceRecord");

        recycle_menu.setLayoutManager(layoutManager);

        search_item = (FloatingActionButton) findViewById(R.id.search_item);

        FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser() ;
        eventInfo = FirebaseDatabase.getInstance().getReference().child("ListOfEvent").child(currentFirebaseUser.getUid());

        loadMenu();

        recycle_menu.addOnItemTouchListener(
                new RecyclerItemClickListener(getBaseContext(), recycle_menu ,new RecyclerItemClickListener.OnItemClickListener() {
                    @Override public void onItemClick(View view, int position) {



                        mDatabaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                                EventName = (TextView) findViewById(R.id.RegisterEventName);
                                if(dataSnapshot.child(EventName.getText().toString()).exists()){
                                    Toast.makeText(StaffAttendanceRecordTable.this, "Got Result", Toast.LENGTH_SHORT).show();

                                    Intent intent =  new Intent(StaffAttendanceRecordTable.this, com.example.edward.neweventmanagementsystem.attendanceListMainList.class);
                                    startActivity(intent);



                                }else{
                                    Toast.makeText(StaffAttendanceRecordTable.this, "No Result", Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) {

                            }
                        });

                    }

                    @Override public void onLongItemClick(final View view, int position) {



                    }
                })
        );

        search_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent search = new Intent(StaffAttendanceRecordTable.this, Find_Event.class);
                startActivity(search);
                System.out.println("Testing search Item");
            }
        });
    }

    public void loadMenu(){
        Query query = eventInfo.orderByKey();
        FirebaseRecyclerOptions firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<EventInfo>().setQuery(query, EventInfo.class).build();
        adapter = new FirebaseRecyclerAdapter<EventInfo, MenuViewHolder>(firebaseRecyclerOptions) {
            @NonNull
            @Override
            public MenuViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_list,viewGroup,false);
                return new MenuViewHolder(view);
            }

            @Override
            protected void onBindViewHolder(@NonNull MenuViewHolder holder, int position, @NonNull EventInfo model) {

                holder.txtRegisterEventId.setText(model.getRegisterEventId());
                holder.txtRegisterEventStartDate.setText(model.getRegisterEventStartDate());
                holder.txtRegisterEventName.setText(model.getRegisterEventName());
                holder.txtRegisterContactNumber.setText(model.getRegisterContactNumber());
                holder.txtRegisterEventRadiogroup.setText(model.getRegisterEventRadiogroup());
                holder.txtRegisterEventLocation.setText(model.getRegisterEventLocation());


                holder.txtEventPrice.setText(model.getEventPrice());
                holder.txtEventCapacity.setText(model.getEventCapacity());

                holder.fileName.setText(model.getFileName());

                //Picasso.with(getBaseContext()).load(model.getImageToUpload()).into(viewHolder.imageView);
                //Glide.with(getBaseContext()).load(model.getImageToUpload()).into(viewHolder.imageView);

                Glide.with(getBaseContext()).load(model.getImageToUpload()
                ).into(holder.imageView);
                System.out.println(model.getRegisterEventName());
                System.out.println(model.getImageToUpload());
            }

        };
        recycle_menu.setAdapter(adapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }
}

Image

1 个答案:

答案 0 :(得分:0)

正如您所说的那样,当您点击addOnItemTouchListener时,您想获取所有事件。

您可以使用此代码。很少修改您的代码。

使用此代码,您将在出勤记录

中获得所有事件名称
    recycle_menu.addOnItemTouchListener(
            new RecyclerItemClickListener(getBaseContext(), recycle_menu ,new RecyclerItemClickListener.OnItemClickListener() {
                @Override public void onItemClick(View view, int position) {

                    DatabaseReference mDatabaseReference;

                    mDatabaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                            ArrayList<String> eventNames = new ArrayList<>();

                            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                                eventNames.add(snapshot.getKey());

                                //Now you have all Event Names in List
                                // you can show them in a Custom Alert Dialog
                                //Or pass it to next Activity.

                            }

                            EventName = (TextView) findViewById(R.id.RegisterEventName);
                            if(dataSnapshot.child(EventName.getText().toString()).exists()){
                                Toast.makeText(StaffAttendanceRecordTable.this, "Got Result", Toast.LENGTH_SHORT).show();

                                Intent intent =  new Intent(StaffAttendanceRecordTable.this, com.example.edward.neweventmanagementsystem.attendanceListMainList.class);
                                startActivity(intent);



                            }else{
                                Toast.makeText(StaffAttendanceRecordTable.this, "No Result", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {

                        }
                    });

                }

                @Override public void onLongItemClick(final View view, int position) {



                }
            })
    );

可以说您已经在“活动/警报”对话框中显示了这些代码,并且用户单击了任何事件。

您可以使用以下代码在活动中全天获取

    databaseReference.child("clicked_item").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            ArrayList<String> eventDays = new ArrayList<>();

            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                eventDays.add(snapshot.getKey());

                //Now you have all days that exist inside Clicked Item (Google)
                //Same as previous you can show them in Alert Dialog or New Activity,

            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

最后一部分。由于您拥有所有的日子,您已经向用户显示了这些日子,用户在任何一天都单击了。

现在您要获取 checkInDate checkInTime 。您可以使用下面的代码来获取此数据。

    databaseReference.child("clciked_Day").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                String checkInDate = snapshot.child("checkInDate").getValue().toString();
                String checkInTime = snapshot.child("checkInTime").getValue().toString();

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

希望这对您有帮助!