Reputation: 251
I'm trying to use the listview using array adapter on my fragment but when trying to do it using the tutorial, It crashes every time i open that specific fragment. I'm trying to show the data on my firebase into the listview but it crashes every time i open it.
From the picture My plan is to only show the email and name of the user and not to include the type but I'll do that later.
And here's my code
public class memberFragment extends Fragment {
private ListView mylistView;
private DatabaseReference mDatabase;
ArrayList<String> myArrayList = new ArrayList<>();
FirebaseDatabase myFirebase;
public memberFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_member2, container, false);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Accounts").child("Users");
final ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,myArrayList);
mylistView = (ListView)view.findViewById(R.id.listView);
mylistView.setAdapter(myArrayAdapter);
mDatabase.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String myChildValues = dataSnapshot.getValue(String.class);
myArrayList.add(myChildValues);
myArrayAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
myArrayAdapter.notifyDataSetChanged();
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
// Inflate the layout for this fragment
return view;
Any ideas what might be causing the problem? I tried to searched and found out its related to hashmap or map, but I have no idea how to put it on my array?
Upvotes: 3
Views: 5147
Reputation: 134
The easier way would be to create an object for the list items you want to retrieve from Firebase and then use that object to set your list view items. But if you are using simple list adapter then I suggest you make the following changes:
mDatabase.addValueEventListener(new ValueEventListener {
@Override
public void onDataChange(DataSnapshot dataSnapshot){
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
String myChildValues = snapshot.getvalue(String.class);
myArrayList.add(myChildValues);
myArrayAdapter.notifyDataSetChange();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Do this accordingly for each of your string data under "Users".
Upvotes: 0
Reputation: 1827
If you want to get all values from reference data snapshot instead of dataSnapshot.getValue(String.class);
you should create data class which contains these fields. As an example:
public class UserData {
private String name;
private String email;
private Integer type;
public UserData() {}
public UserData(String name, String email, Integer type) {
this.name = name;
this.email = email;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
}
Then using this class you can get all data of the user by using it like this:
UserData data = (UserData) dataSnapshot.getValue(UserData.class);
.
And by using getters you can get the value you want.
Upvotes: 3
Reputation: 138834
The can use a model class in order to achieve this or you can use a simplest way, using the String
class. For that, you can use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("Accounts").child("Users");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String email = ds.child("email").getValue(String.class);
String name = ds.child("name").getValue(String.class);
Log.d("TAG", email + " / " + name);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(eventListener);
Your output will be:
[email protected] / idk
//and so on
Upvotes: 4