I've been following several guides most recently this one from CodeLabs to try and create a Room Database for my app. All of the guides I've been using use a Recycler View. I'm trying to figure out how to use this data and implement it in a different type of components but a Spinner for now to display the users.
I've got my entity, dao, database, repository and view model all created and I think sitting correctly.
My adapter differs from the guide as they use code that is for a recyler view. Where as I want my data to appear in a spinner.
class UserListAdapter {
private List<Users> mUser = Collections.emptyList(); // Cached copy of words
void setUser(List<Users> user){
mUser = user;
}
// getItemCount() is called many times, and when it is first called,
// mWords has not been updated (means initially, it's null, and we can't return null).
public int getItemCount() {
if (mUser != null)
return mUser.size();
else return 0;
}}
Where as here is the CodeLabs adapter. I just removed everything related to recyler view and used the remaining code for my adapter.
I have default values "Hello", "World" pre-populating the database during the build in the database class, here's that method:
private static class PopulateDbAsync extends AsyncTask<Void, Void, Void> {
private final UsersDao mDao;
PopulateDbAsync(AppDatabase db) {
mDao = db.usersDao();
}
@Override
protected Void doInBackground(final Void... params) {
mDao.deleteAll();
Users user = new Users("Hello");
mDao.insertNewUser(user);
user = new Users("World");
mDao.insertNewUser(user);
return null;
}
}
}
I have a method which inserts the data into the spinner:
private void createSpinners(ArrayList<String> listToUse) {
ArrayAdapter<String> adp1 = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, listToUse);
spin_PlayerSelection.setAdapter(adp1);
}
In the onCreate code block I want to retrieve the List of users and pass it to the above method.
final UserListAdapter adapter = new UserListAdapter();
mUsersViewModel = ViewModelProviders.of(this).get(UsersViewModel.class);
mUsersViewModel.getAllUsers().observe(this, new Observer<List<Users>>() {
@Override
public void onChanged(@Nullable final List<Users> user) {
// Update the cached copy of the words in the adapter.
// Update scroll view here
adapter.setUser(user);
createSpinners(mUsersViewModel.getAllUsers());
}
});
As it stands now I'm getting the error:
Where am I going wrong?
Updated relecting @JohnJoe's reply:
I tried your way but getAllUsers doesn't have a .Size method because it is LiveData (I think). But I do have a getItemCount method in my UserListAdapter so i used this and then used the get name method in the Users Class which contains the Entity or table, to leave the code like so:
mUsersViewModel.getAllUsers().observe(this, new Observer<List<Users>>() {
@Override
public void onChanged(@Nullable final List<Users> user) {
// Update the cached copy of the words in the adapter.
// Update scroll view here
adapter.setUser(user);
for (int i = 0; i < adapter.getItemCount(); i++) {
playerNames.add(String.valueOf(user.get(i)));
}
createSpinners(sortListAlphabetically(playerNames));
}
});
This returns values but not the names them selves as you can see in this image, but this is progress.
You need to create another ArrayList
used to stored the data from mUsersViewModel.getAllUsers()
.
For an example:
for(i<0;i<mUsersViewModel.getAllUsers().size; i++){
list.add(user.xxx) // get user info here
}
Then only you can pass the list to createSpinner
function.
createSpinners(list);
Based on your edited post, you are getting the list value. You should retrieve variables inside User class. Example: user.get(i).name
where name is inside User
class.