0

I want to get data from a Firebase database in a list view. When I open the page, it appears as follows:

enter image description here

When I go back and open it again, it now appears like this:

enter image description here

The database contains the following data:

enter image description here

My two problems are:

  • It takes a lot of time to retrieve the data
  • More data is retrieved than expected - it is repeated in the listview.

My code:

public class CustomAdapter extends BaseAdapter{
Context c;
ArrayList<Doctor> doctors;

public CustomAdapter(Context c, ArrayList<Doctor> doctors) {
    this.c = c;
    this.doctors = doctors;
}

@Override
public int getCount() {
    return doctors.size();
}

@Override
public Object getItem(int position) {
    return doctors.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView==null)
    {
        convertView= LayoutInflater.from(c).inflate(R.layout.model,parent,false);
    }

    TextView nameTxt= (TextView) convertView.findViewById(R.id.nameTxt);
    TextView propTxt= (TextView) convertView.findViewById(R.id.propellantTxt);
    TextView descTxt= (TextView) convertView.findViewById(R.id.descTxt);

    final Doctor s= (Doctor) this.getItem(position);

    nameTxt.setText(s.getDoctorName());
    propTxt.setText(s.getSpecialist());
    descTxt.setText(s.getAdress());

    //ONITECLICK
    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(c,s.getDoctorName(),Toast.LENGTH_SHORT).show();
        }
    });
    return convertView;
}

and this for model class

public class Doctor {
String doctorName,specialist,adress;

public Doctor() {
}

public String getDoctorName() {
    return doctorName;
}



public String getSpecialist() {
    return specialist;
}


public String getAdress() {
    return adress;
}

The Activity:

public class DoctorsActivity extends AppCompatActivity {
DatabaseReference db;
FirebaseHelper helper;
CustomAdapter adapter;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_doctors);
    lv = (ListView) findViewById(R.id.lv);
    db = FirebaseDatabase.getInstance().getReference().child("Doctor");
    helper = new FirebaseHelper(db);

    adapter = new CustomAdapter(this, helper.retrieve());
    lv.setAdapter(adapter);

}

Firebase Helper:

public class FirebaseHelper {

DatabaseReference db;
Boolean saved;
ArrayList<Doctor> doctors = new ArrayList<>();


public FirebaseHelper(DatabaseReference db) {
    this.db = db;
}


//IMPLEMENT FETCH DATA AND FILL ARRAYLIST
private void fetchData(DataSnapshot dataSnapshot) {
    doctors.clear();

    for (DataSnapshot ds : dataSnapshot.getChildren()) {
        Doctor doctor = dataSnapshot.getValue(Doctor.class);
        doctors.add(doctor);
    }
}

//RETRIEVE
public ArrayList<Doctor> 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 doctors;
}
ColdFire
  • 6,764
  • 6
  • 35
  • 51
  • 2
    Possible duplicate of [How to return dataSnapshot value as a result of a method?](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method) – Alex Mamo Apr 05 '18 at 12:52
  • You cannot return something now that hasn't been loaded yet. You cannot return `doctors` list as a result of the `retrieve` method. Plase see the duplicate for a better understanding. – Alex Mamo Apr 05 '18 at 12:53
  • Please refer to : https://firebase.google.com/docs/database/android/lists-of-data – Deep Patel Apr 05 '18 at 13:37

0 Answers0