簡體   English   中英

在RecyclerView中使用Firebase更新

[英]Update with firebase in RecyclerView

我創建了recyclerview,它具有從Firebase檢索包含名稱,電話,區域,狀態等的數據。 現在,我想用firebase實現itemclicklistener,讓用戶單擊該項目並彈出對話框以更新firebase的狀態值。

我的數據庫為例:

   public SearchViewHolder(View itemView) {
        super(itemView);
        studentName = (TextView) itemView.findViewById(R.id.studentName);
        studentMatrik = (TextView) itemView.findViewById(R.id.studentMatrik);
        studentPhone = (TextView) itemView.findViewById(R.id.studentPhone);
        studentAddress = (TextView) itemView.findViewById(R.id.studentAddress);
        studentStatus = (TextView) itemView.findViewById(R.id.studentStatus);
        studentMail = (TextView) itemView.findViewById(R.id.studentMail);
        studentCon = (TextView) itemView.findViewById(R.id.studentCon);
        checkBox = (CheckBox) itemView.findViewById(R.id.checkBox);
        buttonMap = (Button) itemView.findViewById(R.id.buttonMap);
        buttonMap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MapsViewActivity.class);
                intent.putExtra("Latitude", studentMail.getText().toString());
                intent.putExtra("Longitude", studentCon.getText().toString());
                intent.putExtra("Address", studentAddress.getText().toString());
                context.startActivity(intent);
            }
        });
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

                if (isChecked) {
                    databaseReference = FirebaseDatabase.getInstance().getReference();
                    String name = studentName.getText().toString();
                    databaseReference.child("student").addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                                String id = snapshot.getKey();
                                String studentName = snapshot.child("fullName").getValue(String.class);

                                if(name.equals(studentName)) {

                                }
                                notifyDataSetChanged();
                            }
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });
                } else {

                }
            }
        });
    }
public class Student implements Parcelable{

        private String name;

     //      getter /setter 

   // Parcelable implementation 


}

check Listener您的check Listener

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

                if (isChecked) {
//pass object that contains user info and display that info in Custom Dialog 
UserInfoDIalog infoDialog= new UserInfoDIalog (mContext,userObject);
            infoDialog.show();


    }
}

然后在dialog提交中update該子node

mDatabase.child(userId).setValue(userObject);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM