Search code examples
javaandroidfirebasefirebase-realtime-databasefirebaseui

Can't get data to Listview with FirebaseListAdapter


I could not get FirebaseListAdapter to work, my ListView is always empty in the fragment. What could be the problem?

This is my database: Database

My PirmadienisFragment .xml file:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
tools:context="lt.ivinskis.kligapp3.tvarkarastis.dienos.PirmadienisFragment">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@android:id/list" />
</FrameLayout>

My PirmadienisFragment .class code:

public class PirmadienisFragment extends ListFragment {
    private FirebaseAuth auth;
    private FirebaseUser user;

    public PirmadienisFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

        // DATABASE
        auth = FirebaseAuth.getInstance();
        user = auth.getCurrentUser();

        View view = inflater.inflate(R.layout.fragment_pirmadienis, container, false);
        ListView mListView = (ListView) view.findViewById(android.R.id.list);

        Query query = FirebaseDatabase.getInstance().getReference().child("timetable").child(user.getUid()).child("Pirmadienis");
        FirebaseListOptions<TvarkarastisInformation> options = new FirebaseListOptions.Builder<TvarkarastisInformation>()
            .setQuery(query, TvarkarastisInformation.class)
            .setLayout(android.R.layout.simple_list_item_1)
            .build();

        FirebaseListAdapter mAdapter = new FirebaseListAdapter<TvarkarastisInformation>(options) {
             @Override
             protected void populateView(View view, TvarkarastisInformation model, int position) {
                TextView pamoka = view.findViewById(android.R.id.text1);
                pamoka.setText(model.getPamoka());
            }
        };
        mListView.setAdapter(mAdapter);

        return inflater.inflate(R.layout.fragment_pirmadienis, container, 
false);
    }
}

My TvarkarastisInformation .class file

public class TvarkarastisInformation {
    public String kelinta_pamoka;
    public String kelintadienis;
    public String pamoka;

    public TvarkarastisInformation() {}

    public TvarkarastisInformation(String kelinta_pamoka, String kelintadienis, String pamoka) {
        this.kelinta_pamoka = kelinta_pamoka;
        this.kelintadienis = kelintadienis;
        this.pamoka = pamoka;
    }

    public String getKelinta_pamoka() {
        return kelinta_pamoka;
    }

    public String getKelintadienis() {
        return kelintadienis;
    }

    public String getPamoka() {
        return pamoka;
    }

    public void setKelinta_pamoka(String kelinta_pamoka) {
        this.kelinta_pamoka = kelinta_pamoka;
    }

    public void setKelintadienis(String kelintadienis) {
        this.kelintadienis = kelintadienis;
    }

    public void setPamoka(String pamoka) {
        this.pamoka = pamoka;
    }
}

Adding my data to the database:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tvarkarastis_add);

    databaseReference = FirebaseDatabase.getInstance().getReference();
    auth = FirebaseAuth.getInstance();
    user = auth.getCurrentUser();

    et_pam_nr = (EditText) findViewById(R.id.kelinta_pamoka);
    et_pam_pav = (EditText) findViewById(R.id.pamokos_pavadinimas);
    et_pam_kelintad = (EditText) findViewById(R.id.kelintadienis);
}

private void writeNewTimetable(String kelinta_pamoka, String kelintadienis, String pamoka) {

    TvarkarastisInformation tvarkarastisInformation = new TvarkarastisInformation(kelinta_pamoka, kelintadienis, pamoka);
    databaseReference.child("timetable").child(user.getUid()).child(kelintadienis).child(kelinta_pamoka).setValue(pamoka);
}

private void addLesson() {
    TvarkarastisInformation tvarkarastisInformation = new TvarkarastisInformation();

    String pamoka = et_pam_pav.getText().toString();
    String kelintadienis = et_pam_kelintad.getText().toString();
    String kelinta_pamoka = et_pam_nr.getText().toString();

    tvarkarastisInformation.setPamoka(pamoka);
    tvarkarastisInformation.setKelintadienis(kelintadienis);
    tvarkarastisInformation.setKelinta_pamoka(kelinta_pamoka);

    writeNewTimetable(kelinta_pamoka, kelintadienis, pamoka);
}

Thank you for your help.


Solution

  • In order to solve this, first make the following fileds private:

    private String kelinta_pamoka;
    private String kelintadienis;
    private String pamoka;
    

    The way in which you read the data is correct but the way you are adding data is not correct and for that I recommend you replace addLesson() method and writeNewTimetable() method with:

    private void addLesson() {
        String pamoka = et_pam_pav.getText().toString();
        String kelintadienis = et_pam_kelintad.getText().toString();
        String kelinta_pamoka = et_pam_nr.getText().toString();
        TvarkarastisInformation tvarkarastisInformation = new TvarkarastisInformation(pamoka, kelintadienis, kelinta_pamoka);
    
        DatabaseReference pirmadienisRef = databaseReference.child("timetable").child(user.getUid()).child("Pirmadienis");
        String key = pirmadienisRef.push().getKey();
        pirmadienisRef.child(key).setValue(tvarkarastisInformation);
    }
    

    You have already created that object, there is no need to create it twice. Remove the old data, add fresh data and try again.