0

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.

Yamiess
  • 251
  • 4
  • 15
  • Is this also happening when you rotating the display? – Alex Mamo Feb 21 '18 at 16:56
  • Yes, it would also become blank when I rotate the display – Yamiess Feb 21 '18 at 17:01
  • Possible duplicate of [Android ListView adapter not pushing individual items for chat app - Firebase-Ui 3.1](https://stackoverflow.com/questions/47228262/android-listview-adapter-not-pushing-individual-items-for-chat-app-firebase-ui) – Peter Haddad Feb 21 '18 at 17:42
  • @Yamiess check the link above, it will solve your problem – Peter Haddad Feb 21 '18 at 17:42
  • Let me see. 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 http://prntscr.com/ihx3fg where the listview is visible. into http://prntscr.com/ihx3th where the listview disappeared. And i tried the codes on the link above. Does it seem like I need to create another method on my user class for the startlistening? – Yamiess Feb 21 '18 at 17:53

2 Answers2

1

This behaviour is present because of the adapter which is disconected every time the onStop() method is called.

To solve this, you need to set the adapter again in the onStart() method.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thank you for your answer sir, and I'm really sorry for this question but where can i see my onstop and on start method to set my adapter?? – Yamiess Feb 21 '18 at 17:13
  • If you don't have those methods in your activity yet, you need to implement them. Hit `ALT + Insert` in Android Studio, choose `Override Methods` and add both methods. – Alex Mamo Feb 21 '18 at 17:15
  • Nope, Its still the same, blank. albiet when i keep rotating the screen, the data shows up again but disappears when I rotate again. (it also does this even without onStart – Yamiess Feb 21 '18 at 17:34
  • Ok, you connect the adapter but be sure is populated. If it's empty, doesn't help you. – Alex Mamo Feb 21 '18 at 17:38
  • Its populated http://prntscr.com/ihx3fg , But when i rotate the screen again it becomes empty like this http://prntscr.com/ihx3th – Yamiess Feb 21 '18 at 17:42
  • In this I suggest you using `FirebaseRecyclerOptions` ans `FirebaseRecyclerAdapter`. The last one will handle fir you all these problems. – Alex Mamo Feb 21 '18 at 17:52
1

Solved it by adding a child event listener

ex.

db.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            lv.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }
Yamiess
  • 251
  • 4
  • 15