我正在尝试使用Firebase数据库中的User对象填充ListView。我见过有人使用RecyclerView,但无法在Android Studio上使用RecyclerView(也许我使用的是不兼容的依赖项)。
无论如何,这是我的代码:
// necessary imports
public class List extends AppCompatActivity {
ListView usersList;
private DatabaseReference userDatabase;
ArrayList<String> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
usersList = (ListView) findViewById(R.id.list_users);
userDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
}
@Override
protected void onStart() {
super.onStart();
// Populate ArrayAdapter with User objects from Firebase Database.
ArrayAdapter<Users> Adapter = new ArrayAdapter<Users>(this, Users.class, android.R.layout.simple_expandable_list_item_1, userDatabase){
// Populate here, don't know what to do.
};
usersList.setAdapter(Adapter);
usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Users.connectTo = list.get(i);
Intent chatActivity = new Intent(List.this, ChatActivity.class);
startActivity(chatActivity);
}
});
}
}