DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("Users");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<String> al = new ArrayList<>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String rId = ds.child("Phone_Number").getValue(String.class);
String name = ds.child("Name").getValue(String.class);
al.add(name);
al.add(rId);
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, al);
ListView listView = rootView.findViewById(R.id.usersList);
listView.setAdapter(adapter);
mProgressDialogue.dismiss();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getActivity(), "Please Check Your Internet Connection", Toast.LENGTH_SHORT).show();
}
};
usersRef.addListenerForSingleValueEvent(eventListener);
usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//This cannot be done in this way
Intent intent = new Intent(getActivity(), Chat.class);
String rId = (String)parent.getItemAtPosition(position);
String name = (String)parent.getItemAtPosition(position);
intent.putExtra("Recievers_Id", rId);
intent.putExtra("Recievers_Name", name);
startActivity(intent);
}
});
return rootView;
就像。在列表视图中......显示了相同的用户userid和名称...显示的是。因为我已经添加了al.add(name)和id ...这是因为... onclicklistener我想获取用户的id,但listview应该包含名称......我无法做到这一点。 ..如果我点击用户的名称,它会取名字,如果我点击用户的id,它就会获取id ...我希望它的工作方式......如果我点击用户的名字,它应该是FETCH ID