简体   繁体   English

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

[英]How to Delete the Firebase database Key from Recycle view based on position

I'm building an app as college project for Blood Donation where user can register using Firebase authentication Email and Password and can post request for Blood which is then added to Firebase DataBase which is shown in Recycle view inside an app I'was able to complete that part.我正在构建一个应用程序作为献血的大学项目,其中用户可以使用 Firebase 身份验证电子邮件和密码进行注册,并且可以发布对血液的请求,然后将其添加到 Firebase 数据库中,该数据库显示在应用程序内的回收视图中,我能够完成那部分。

Now I want to add button which will be visible in the recycle view with option to delete the request if He wish in case he received call from donor or if he no longer in need.现在我想添加一个按钮,该按钮将在回收视图中可见,如果他希望收到捐赠者的电话或不再需要,他可以选择删除请求。 But this button should only be visible to user who has posted for request and will remain hidden for other help post inside recycle adapter.但是这个按钮应该只对发布请求的用户可见,并且对于回收适配器内的其他帮助发布将保持隐藏。

and when he click the button it should delete the particular Data from Firebase Database also.当他点击按钮时,它也应该从 Firebase 数据库中删除特定的数据。

To summarize.总结一下。 1. I need button visible only to user who has posted the help request which will delete the post if he wish to 1.我需要一个只有发布帮助请求的用户才能看到的按钮,如果他愿意,这个按钮会删除帖子

  1. On click event of that Button, will also delete the particular post from Firebase Database在该按钮的点击事件中,也会从 Firebase 数据库中删除特定的帖子

I'm using Firebase authentication sing in of Email and Password.我正在使用 Firebase 身份验证在电子邮件和密码中唱歌。

Here is the Firebase Database Structure这是 Firebase 数据库结构

Here is the Code where user wants to upload the Help request and is added to firebase Data as well这是用户想要上传帮助请求并添加到 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();
                }
            }
        });


    }
}

Here is the Code where list of help post is show including user who has posted of his/her own这是显示帮助帖子列表的代码,包括发布了他/她自己的用户

 * 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){


        }
    }

}

Any help is well appreciated任何帮助都非常感谢

Thanks in advance.提前致谢。

Add delete button in your help_single_layout .help_single_layout 中添加删除按钮。 And add a new key posted in firebase database to check whether this user posted or not .并在 firebase 数据库中添加一个发布的新密钥以检查该用户是否发布。 On that basis , you can define visibiltiy of Delete button.在此基础上,您可以定义删除按钮的可见性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM