无法使用FirebaseListAdapter将数据传输到Listview

时间:2018-04-03 17:40:13

标签: java android firebase firebase-realtime-database firebaseui

我无法让FirebaseListAdapter工作,我的ListView在片段中始终为空。可能是什么问题?

这是我的数据库: Database

我的PirmadienisFragment .xml文件:

<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>

我的PirmadienisFragment .class代码:

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);
    }
}

我的Tvarkarastis信息.class文件

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;
    }
}

将我的数据添加到数据库:

@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);
}

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

为了解决这个问题,首先将以下文件设为私有:

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

您阅读数据的方式是正确的,但您添加数据的方式不正确,为此,我建议您将addLesson()方法和writeNewTimetable()方法替换为:

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);
}

您已经创建了该对象,无需创建它两次。删除旧数据,添加新数据,然后重试。

答案 1 :(得分:1)

添加:

bot.recognizer(recognizer);
bot.dialog("/", [ ... ]);

bot.dialog("luisIntentA", [ ... ])
   .triggerAction({
     matches: "luisIntentA"
   });

在此之下:

mAdapter.startListening();

开始收听数据。

更多信息:

https://github.com/firebase/FirebaseUI-Android/tree/master/database#firebaserecycleradapter-lifecycle