[英]How to fill ListView Adapter items with data from FirebaseDatabase [Android Studio]
I'm building an app which shows data from firebase database in ListView
adapter, but the listview doesn't fill with data. 我正在构建一个应用,该应用在
ListView
适配器中显示Firebase数据库中的数据,但listview中没有填充数据。 Database works. 数据库有效。 I tried this with
textview.setText
method on specially created layout. 我在专门创建的布局上使用
textview.setText
方法尝试了此操作。
public class MainActivityListFragment extends ListFragment {
private ArrayList<typ> typy;
private typAdapter typAdapter;
String tytul1, rodzaj1, godzina1, typ1, kurs1;
DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference mmecz1 = mRootRef.child("mecz1");
DatabaseReference mtytul1 = mmecz1.child("tytul1");
DatabaseReference mrodzaj1 = mmecz1.child("rodzaj1");
DatabaseReference mgodzina1 = mmecz1.child("godzina1");
DatabaseReference mtyp1 = mmecz1.child("typ1");
DatabaseReference mkurs1 = mmecz1.child("kurs1");
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
typy = new ArrayList<typ>();
typy.add(new typ(tytul1, rodzaj1, godzina1, typ1, kurs1));
typy.add(new typ("Portugalia - Islandia", "wygrana drużyny", "21:00", "1", "1.50"));
typAdapter = new typAdapter(getActivity(), typy);
setListAdapter(typAdapter);
getListView().setDivider(ContextCompat.getDrawable(getActivity(), android.R.color.black));
getListView().setDividerHeight(0);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
}
@Override
public void onStart() {
super.onStart();
mtytul1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text1 = dataSnapshot.getValue(String.class);
tytul1 = text1;
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getActivity(),"No internet connection",Toast.LENGTH_SHORT).show();
}
});
mrodzaj1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text2 = dataSnapshot.getValue(String.class);
rodzaj1=text2;
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mgodzina1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text3 = dataSnapshot.getValue(String.class);
godzina1 = text3;
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mtyp1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text4 = dataSnapshot.getValue(String.class);
typ1 = text4;
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mkurs1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text5 = dataSnapshot.getValue(String.class);
kurs1 = text5;
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
You need to use a FirebaseListAdapter. 您需要使用FirebaseListAdapter。
Add this dependency to your project: 将此依赖项添加到您的项目中:
compile 'com.firebaseui:firebase-ui-database:0.4.0'
Then follow these instruction from their Github readme: 然后按照他们的Github自述文件中的说明进行操作:
https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md
This isn't working because the values in your Strings are null when the typy.add()
is executed. 这不起作用,因为当执行
typy.add()
时,字符串中的值为null。
For achieving what you want, you have to figure out a way to add your strings in typy.add()
inside your .addValueEventListener
and calling typAdapter.notifyDataSetChanged()
later. 为了实现所需的功能,您必须找出一种在
typy.add()
内部的.addValueEventListener
typy.add()
添加字符串并稍后调用typAdapter.notifyDataSetChanged()
。
You can also use Firebase UI, which has FirebaseListAdapters, if your ListView is simple. 如果您的ListView很简单,您也可以使用具有FirebaseListAdapters的Firebase UI。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.