如何根据位置从回收视图中删除Firebase数据库密钥

时间:2018-09-03 16:12:09

标签: java android firebase firebase-realtime-database

我正在为大学献血项目构建一个应用程序,用户可以在其中使用Firebase身份验证电子邮件和密码进行注册,并可以发布对血液的请求,然后将其添加到Firebase数据库中,该信息显示在应用程序内的“回收”视图中 我能够完成那部分。

现在,我想添加一个按钮,该按钮将在回收视图中显示,并带有删除请求的选项,如果他希望他收到捐助者的来电或不再需要他的话。但是此按钮仅对已发布请求的用户可见,而对于回收适配器内的其他帮助发布将保持隐藏。

当他单击按钮时,它也应该从Firebase数据库中删除特定数据。

总结。 1.我需要仅对发布了帮助请求的用户可见的按钮,如果愿意,该按钮将删除该帖子

  1. 在该按钮的单击事件上,还将从Firebase数据库中删除特定帖子

我正在使用Firebase身份验证输入电子邮件和密码。

这是Firebase数据库结构

此处是用户要上传帮助请求并添加到的代码 也可以火力扑灭数据

public class EnquiryActivity extends AppCompatActivity {

    //UI
    Button btnRequest;
    EditText edtName,edtBlood,edtPlace,edtMobile,edtEmail;
    //DB
    DatabaseReference mHelper;
    FirebaseAuth mAuth;
    //progress
    ProgressDialog mProgress;
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enquiry);
        //initialisation
        edtBlood=(EditText)findViewById(R.id.enq_blood);
        edtMobile=(EditText)findViewById(R.id.enq_mobile);
        edtEmail=(EditText)findViewById(R.id.enq_email);
        edtName=(EditText)findViewById(R.id.enq_name);
        edtPlace=(EditText)findViewById(R.id.enq_place);
        btnRequest=(Button)findViewById(R.id.button2);
        //firebase
        mHelper= FirebaseDatabase.getInstance().getReference();
        final String mCurrentUser=FirebaseAuth.getInstance().getCurrentUser().getUid().toString();
        mAuth=FirebaseAuth.getInstance();
        //progress
        mProgress=new ProgressDialog(this);
        mProgress.setTitle("Loading");
        mProgress.setMessage("Please wait..");

        btnRequest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mProgress.show();
                String blood=edtBlood.getText().toString();
                String name=edtName.getText().toString();
                String mobile=edtMobile.getText().toString();
                String email=edtMobile.getText().toString();
                String  place=edtPlace.getText().toString();
                String temp=blood.toUpperCase();
                if(!TextUtils.isEmpty(blood)||!TextUtils.isEmpty(name)||!TextUtils.isEmpty(mobile)||
                        !TextUtils.isEmpty(place)){

                        HashMap<String, String> userMap = new HashMap<>();
                        userMap.put("name", name);
                        userMap.put("blood_group","Blood Group:- " + blood);
                        userMap.put("email", email);
                        userMap.put("mobile", mobile);
                        userMap.put("place","Location:- " + place);

                        mHelper.child("Help").child(mCurrentUser).setValue(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                mProgress.dismiss();
                                Toast.makeText(getApplicationContext(), "Registered Successfully..!", Toast.LENGTH_LONG).show();
                                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                startActivity(intent);
                            }
                        });

            }else{
                    Toast.makeText(getApplicationContext(),"Please enter the details in all fields",Toast.LENGTH_LONG).show();
                }
            }
        });


    }
}

此处是显示帮助帖子列表的代码,包括自己张贴的用户

 * A simple {@link Fragment} subclass.
 */
public class NeedHelpFragment extends Fragment {

    FloatingActionButton floatingActionButton;
    private View mMainView;
    private RecyclerView mHelpList;
    private DatabaseReference mUsersDatabase;
    private DatabaseReference mUsers;

    private FirebaseAuth mAuth;

    private String mCurrent_user_id;

    public NeedHelpFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mMainView = inflater.inflate(R.layout.fragment_need_help, container, false);
        floatingActionButton = (FloatingActionButton) mMainView.findViewById(R.id.float_add);
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), EnquiryActivity.class));
            }
        });

        //init
        mHelpList = (RecyclerView) mMainView.findViewById(R.id.need_recyclerview);
        mAuth = FirebaseAuth.getInstance();
        mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Help");
        mUsers = FirebaseDatabase.getInstance().getReference().child("Users");
        mCurrent_user_id = mAuth.getCurrentUser().getUid();

        //
        mHelpList.setHasFixedSize(true);
        LinearLayoutManager linearVertical = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        mHelpList.setLayoutManager(linearVertical);

        DividerItemDecoration mDividerItemDecoration = new DividerItemDecoration(
                mHelpList.getContext(),
                linearVertical.getOrientation()
        );
        mHelpList.addItemDecoration(mDividerItemDecoration);
        return mMainView;
    }

    @Override
    public void onStart() {
        super.onStart();

        FirebaseRecyclerAdapter<Help, HelpViewHolder> friendsRecyclerViewAdapter = new FirebaseRecyclerAdapter<Help, HelpViewHolder>(

                Help.class,
                R.layout.help_single_layout,
                HelpViewHolder.class,
                mUsersDatabase) {
            @Override
            protected void populateViewHolder(final HelpViewHolder helpViewHolder, Help help, int i) {

                helpViewHolder.setDate(help.getDate());

                final String list_user_id = getRef(i).getKey();

                mUsersDatabase.child(list_user_id).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        final String userName = dataSnapshot.child("name").getValue().toString();
                        String blood = dataSnapshot.child("blood_group").getValue().toString();
                        final String phone = dataSnapshot.child("mobile").getValue().toString();
                        final String email = dataSnapshot.child("email").getValue().toString();
                        String address = dataSnapshot.child("place").getValue().toString();


                        helpViewHolder.setName(userName);
                        helpViewHolder.setBlood(blood);
                        helpViewHolder.setAddress(address);
                        helpViewHolder.setPhone(phone);
                        helpViewHolder.setEmail(email);
                        helpViewHolder.mView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {

                                CharSequence options[] = new CharSequence[]{"Email", "Call"};

                                final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());

                                builder.setTitle("Select Options");
                                builder.setItems(options, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {

                                        //Click Event for each item.
                                        if (i == 0) {

                                        }

                                        if (i == 1) {

                                            String uri = phone;


                                            if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {



                                            }

                                        }

                                    }
                                });

                                builder.show();

                            }
                        });


                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

            }
        };

        mHelpList.setAdapter(friendsRecyclerViewAdapter);


    }

    // viewholder class..

    public static class HelpViewHolder extends RecyclerView.ViewHolder {
        View mView;
        public HelpViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
        }
        public void setBlood(String blood){
            TextView userStatusView = (TextView) mView.findViewById(R.id.help_blood);
            userStatusView.setText(blood.toUpperCase());
        }
        public void setName(String name){

            TextView userNameView = (TextView) mView.findViewById(R.id.help_name);
            userNameView.setText(name.toUpperCase());
        }
        public void setPhone(String phone){

            TextView userNameView = (TextView) mView.findViewById(R.id.help_mobile);
            userNameView.setText(phone);
        }


        public void setEmail(String name){

            TextView userNameView = (TextView) mView.findViewById(R.id.help_email);
            userNameView.setText(name.toUpperCase());
        }


        public void setAddress(String address) {

            TextView userNameView = (TextView) mView.findViewById(R.id.help_place);
            address.toUpperCase();
            userNameView.setText(address.toUpperCase());
        }
        public void setDate(String date){


        }
    }

}

我们非常感谢您的帮助

谢谢。

1 个答案:

答案 0 :(得分:0)

help_single_layout 中添加删除按钮。并在Firebase数据库中添加已发布的新密钥,以检查该用户是否已发布。在此基础上,您可以定义删除按钮的可见性。