Hello there I have a fragment and then a listview on it. It would normally load the listview data when application is launched, but when i click the fragment again using the navigation drawer, all of it would disappear and would leave a blank page as a result.
My listview does indeed show itself but the problem is when I rotate the screen or initiate the fragment again, the listview disappears. ex. from prntscr.com/ihx3fg where the listview is visible. into prntscr.com/ihx3th where the listview disappeared.
Can I ask why this is happening?
My CustomAdapter code
public class CustomAdapter extends BaseAdapter {
Context c;
ArrayList<User1> user;
public CustomAdapter(Context c, ArrayList<User1> user) {
this.c = c;
this.user = user;
}
@Override
public int getCount() {
return user.size();
}
@Override
public Object getItem(int pos) {
return user.get(pos);
}
@Override
public long getItemId(int pos) {
return pos;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
if(convertView==null){
convertView= LayoutInflater.from(c).inflate(R.layout.model,viewGroup,false);
}
TextView nameTxt= (TextView) convertView.findViewById(R.id.nameTxt);
TextView emailTxt= (TextView) convertView.findViewById(R.id.emailTxt);
TextView dateTxt= (TextView) convertView.findViewById(R.id.dateTxt);
final User1 user= (User1) this.getItem(position);
nameTxt.setText(user.getName());
emailTxt.setText(user.getEmail());
dateTxt.setText(user.getDate());
return convertView;
}
Firebasehelper Code
public class Firebasehelper {
DatabaseReference db;
Boolean saved=null;
ArrayList<User1> users=new ArrayList<>();
public Firebasehelper(DatabaseReference db) {
this.db = db;
}
public Boolean save(User1 user) {
if (user == null) {
saved = false;
} else {
try {
db.child("Accounts").child("Transaction").push().setValue(user);
saved = true;
} catch (DatabaseException e) {
e.printStackTrace();
saved = false;
}
}
return saved;
}
private void fetchData(DataSnapshot dataSnapshot)
{
User1 user=dataSnapshot.getValue(User1.class);
users.add(user);
}
public ArrayList<User1>retrieve(){
db.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return users;
My Fragment Activity Code
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_member2, container, false);
lv = (ListView) view.findViewById(R.id.lv);
db= FirebaseDatabase.getInstance().getReference().child("Accounts").child("Users");
helper=new Firebasehelper(db);
adapter=new CustomAdapter(getActivity(),helper.retrieve());
lv.setAdapter(adapter);
// Inflate the layout for this fragment
return view;
}
My User Class
public User1() {
this.name=name;
this.email=email;
this.date=date;
}
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 String getDate(){
return date;
}
public void setDate(String date){
this.date = date;
}
Really confused on where the problem lies in since the app is running perfectly except for the fact that the fragments listview would disappear or became blank when initiating the fragment again.