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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何根据位置从“回收”视图中删除Firebase数据库密钥相关的知识,希望对你有一定的参考价值。

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

现在我想添加一个按钮,该按钮在循环视图中可见,如果他愿意,如果他接到捐赠者的电话或者他不再需要,可以选择删除请求。但是,此按钮应仅对已发布请求的用户可见,并且将保留为在回收适配器内的其他帮助发布时隐藏。

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

总结一下。 1.我只需要向已发布帮助请求的用户显示按钮,如果他愿意,将删除该帖子

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

我正在使用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){


        }
    }

}

任何帮助都非常感谢

提前致谢。

答案

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

以上是关于如何根据位置从“回收”视图中删除Firebase数据库密钥的主要内容,如果未能解决你的问题,请参考以下文章

如何使用随机键从 Firebase 数据库中检索子项到 Android 的回收站视图中?

使用警报对话框时在回收站视图中从 Firebase 中删除项目

想要删除recycleview上的一个项目。单击按钮删除单击。该项目在firebase数据库中删除但在循环视图中该项目存在

无法从 firebase 回收器视图中的 firebase 实时数据库中获取嵌套数据

回收站视图没有滚动到最后一个位置

使用firebase在无尽的回收器视图中进行分页