好的,所以我在我的应用程序中有两个模块。一个是用户,另一个是管理员。当用户注册自己时,管理员必须基本上批准用户的注册,为此,我做了一个简单的逻辑,当用户注册时,他们注册了一个额外的值userStatus = "0"
,和管理员时,点击批准按钮的值更改为“1”,他们被允许登录。我正在使用一个Recycler视图来向管理模块显示数据库中的所有用户,但我想更改一个不确定如何访问的值,
下面是我的firebase数据库结构
(https://i.stack.imgur.com/LmYvR.png)complete picture of firebase database for clarity
下面是RecyclerView适配器类的代码
package com.example.adminclient;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
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.ValueEventListener;
import java.util.ArrayList;
public class AdapterRecycler extends RecyclerView.Adapter<AdapterRecycler.MyViewHolder> {
Context context;
ArrayList<UserData> list;
FirebaseDatabase fDB = FirebaseDatabase.getInstance();
DatabaseReference reference = fDB.getReference();
public AdapterRecycler(Context context, ArrayList<UserData> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.users,parent,false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
UserData userData = list.get(position);
holder.firstName.setText(userData.getFirstName());
holder.address.setText(userData.getAddress());
holder.emailAddress.setText(userData.getEmailAddress());
holder.userStatus.setText(userData.getUserStatus());
}
@Override
public int getItemCount() {
return list.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView firstName, address, emailAddress, userStatus;
Button approveUser;
FirebaseDatabase fDB = FirebaseDatabase.getInstance();
DatabaseReference reference = fDB.getReference();
public MyViewHolder(@NonNull View itemView) {
super(itemView);
firstName = itemView.findViewById(R.id.firstNametv);
address = itemView.findViewById(R.id.addresstv);
emailAddress = itemView.findViewById(R.id.emailtv);
approveUser = itemView. findViewById(R.id.approve);
userStatus = itemView.findViewById(R.id.userStatustv);
// String firstNamedb = firstName.getText().toString();
approveUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fDB.getReference().child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
reference.child("Users").child(???).child("userStatus").setValue("1");
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
});
}
}
}
Here's a screenshot of Recycler View in app
下面是Recycler View类的代码
package com.example.adminclient;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
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.ValueEventListener;
import java.util.ArrayList;
public class VerifyUsers extends AppCompatActivity {
RecyclerView recyclerView;
DatabaseReference databaseReference;
AdapterRecycler adapterRecycler;
ArrayList<UserData> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verify_users);
recyclerView = findViewById(R.id.userVerify);
databaseReference = FirebaseDatabase.getInstance().getReference("Users");
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
list = new ArrayList<>();
adapterRecycler = new AdapterRecycler(this, list);
recyclerView.setAdapter(adapterRecycler);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()){
UserData userData = dataSnapshot.getValue(UserData.class);
list.add(userData);
}
adapterRecycler.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
如果有谁了解我处境和问题,请帮助我。
我尝试了我可以在'approve'按钮onclicklistener上添加一个值事件侦听器,并从firebase数据库引用中检索孩子,但我不知道如何从这里向前迈进。
approveUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fDB.getReference().child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
reference.child("Users").child(???).child("userStatus").setValue("1");
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
});
下面是我的UserData类
package com.example.adminclient;
import android.location.Address;
public class UserData {
String firstName;
String address;
String emailAddress;
String userStatus;
public UserData() {
}
public String getFirstName() {
return firstName;
}
public String getAddress() {
return address;
}
public String getEmailAddress() {
return emailAddress;
}
public String getUserStatus() {
return userStatus;
}
}
2条答案
按热度按时间oaxa6hgo1#
最简单的解决方案是将整个
UserData
对象从onBindViewHolder
传递给我在MyViewHolder
类中添加的bindData
函数:一旦你有了
UserData
,你就可以创建一个指向userStatus
字段的引用,并改变它的值:8ftvxx2r2#
尝试下面的代码行。