尝试显示来自firebase的数据列表时,Android应用程序崩溃
public class exercisehowactivity extends Activity {
private DatabaseReference mDatabase;
private Button btnSearch;
private EditText actSearch;
private ListView lvexercise;
Exercise exercise;
String key;
private ArrayList<String> arrayList = new ArrayList <> ();
private ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_exerciseshow );
mDatabase = FirebaseDatabase.getInstance ().getReference ().child("Exercise").child(key).child("name");
// DatabaseReference commandsRef = mDatabase.child(&#34; Exercise&#34;);
adapter = new ArrayAdapter <String> ( this, android.R.layout.simple_list_item_1, arrayList );
btnSearch = (Button) findViewById ( R.id.btnSearch );
actSearch = (EditText) findViewById ( R.id.actSearch );
lvexercise = (ListView) findViewById ( R.id.lvexercise );
mDatabase.addChildEventListener ( new ChildEventListener () {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
// Map map =(Map)dataSnapshot.getValue(); // String string = dataSnapshot.getValue(String.class);
Exercise latest = dataSnapshot.getValue(Exercise.class);
arrayList.add ( String.valueOf ( latest ) );
adapter.notifyDataSetChanged ();
lvexercise.setAdapter ( adapter );
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
} );
}
}
答案 0 :(得分:0)
您无需添加ChildEventListener。有一个firebase库可用于使用数据库中的数据填充listview。
这是链接,它还有一个指南。
https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md