我想在UserList中选择displayName show。但我不知道如何在数据库用户中选择displayName你能帮助我吗
例如。我选择6Z86LIYRMaM9vtmOrd23kAeICO63
,但我想显示displayName
ListView usersList;
TextView noUsersText;
ArrayList<String> al = new ArrayList<>();
int totalUsers = 0;
ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_users);
usersList = (ListView)findViewById(R.id.usersList);
noUsersText = (TextView)findViewById(R.id.noUsersText);
pd = new ProgressDialog(Users.this);
pd.setMessage("Loading...");
pd.show();
String url = "https://chatapp.firebaseio.com/users.json";
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>(){
@Override
public void onResponse(String s) {
doOnSuccess(s);
}
},new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError volleyError) {
System.out.println("" + volleyError);
}
});
RequestQueue rQueue = Volley.newRequestQueue(Users.this);
rQueue.add(request);
usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
UserDetails.chatWith = al.get(position);
startActivity(new Intent(Users.this, Chat.class));
}
});
}
public void doOnSuccess(String s){
try {
JSONObject obj = new JSONObject(s);
Iterator i = obj.keys();
String key = "";
while(i.hasNext()){
key = i.next().toString();
if(!key.equals(UserDetails.username)) {
al.add(key);
}
totalUsers++;
}
} catch (JSONException e) {
e.printStackTrace();
}
if(totalUsers <=1){
noUsersText.setVisibility(View.VISIBLE);
usersList.setVisibility(View.GONE);
}
else{
noUsersText.setVisibility(View.GONE);
usersList.setVisibility(View.VISIBLE);
usersList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, al));
}
pd.dismiss();
}
答案 0 :(得分:0)
根据评论中的建议,您应该访问firebase文档,了解如何使用SDK。
https://firebase.google.com/docs/database/android/start/
特定于您的问题,您需要为用户创建POJO类,以便稍后在从db中检索用户时序列化对象。
User.java
public class User {
private int avatarId;
private String connection;
private Long createAt;
private String displayName;
private String email;
public User() {
}
public int getAvatarId() {
return avatarId;
}
public void setAvatarId(int avatarId) {
this.avatarId = avatarId;
}
public String getConnection() {
return connection;
}
public void setConnection(String connection) {
this.connection = connection;
}
public Long getCreateAt() {
return createAt;
}
public void setCreateAt(Long createAt) {
this.createAt = createAt;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
然后你可以像这样从db中获取用户信息。
DatabaseReference databaseRef = FirebaseDatabase.getInstance().getReference()
DatabaseReference userReference = databaseRef.child("users").child("6Z86LIYRMaM9vtmOrd23kAeICO63");
ValueEventListener userListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Create a User POJO class with the same values you have in your db
//and its getters and setters
User user = dataSnapshot.getValue(User.class);
//This is how you get the displayName from a user from the db
user.getDisplayName();
// ...
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
userReference.addValueEventListener(userListener);
我希望这会有所帮助