RecyclerView项目未填充(AndroidStudio,Room,MVVM)

时间:2019-04-27 05:39:35

标签: android android-recyclerview

这个周末,在两场史诗般的战斗之外,我还有一个悲剧。我正在尝试为我的投资组合创建一个健身追踪应用。

在该应用程序中,一切似乎都很好-AndroidStudios的模拟器清楚地显示了RecyclerView元素(我将深色背景和奇怪的边距设置为清晰可见)。并且数据库似乎正在正确填充。但是不会出现任何内容填充RecyclerView。在这一点上,我只是试图让他们阅读“你好”,但他们最终将需要存储客户端数据。 (为了简化问题,尝试从后一种情况切换到前一种情况时,我可能会感到困惑。)

AdminListActivity

public class AdminListActivity extends AppCompatActivity {
    public static final int NEW_USER_ACTIVITY_REQUEST_CODE = 1;
    private ViewModel mViewModel;
    private List<User> mUsers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_admin_list);

        RecyclerView recyclerView = findViewById(R.id.admin_recycler_view);
        final UserListAdapter adapter = new UserListAdapter(this, mUsers);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        // Get ViewModel from ViewModelProvider
        mViewModel = ViewModelProviders.of(this).get(ViewModel.class);

        // Observer on LiveData returned by getAllUsers.
        mViewModel.getAllUsers().observe(this, new Observer<List<User>>() {
            @Override
            public void onChanged(@Nullable final List<User> users) {
                // Update the cached copy of the words in the adapter.
                adapter.setUsers(users);
            }
        });
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == NEW_USER_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
            User user = new User(data.getStringExtra(RegisterFragment.EXTRA_REPLY));
            mViewModel.addUser(user);
        } else {
            Toast.makeText(
                    getApplicationContext(),
                    "not saved",
                    Toast.LENGTH_LONG).show();
        }
    }
}

AdminListFragment


public class AdminListFragment extends Fragment implements View.OnClickListener {

    private RecyclerView mUserRecyclerView;
    private Button mAddNewClient;

    public AdminListFragment() {}

    @Override
    public void onClick(View v) {
        // required onClick method
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_admin_list, container, false);

        mUserRecyclerView = (RecyclerView) view.findViewById(R.id.admin_recycler_view);
        mUserRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        setHasOptionsMenu(true);
        mAddNewClient = view.findViewById(R.id.add_new_client);
        return view;

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
        inflater.inflate(R.menu.fragment_admin_list, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.add_new_client:              SingleFragmentActivity.fm.beginTransaction().replace(R.id.fragment_container,
                        new RegisterFragment()).addToBackStack(null).commit();
                // TODO: OR: add from contacts
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

UserListAdapter

public class UserListAdapter extends RecyclerView.Adapter<UserListAdapter.UserViewHolder> {
    private LayoutInflater mInflater = null;
    private List<User> mUsers = Collections.emptyList(); // Cached copy of users

    public UserListAdapter(Context context, List<User> mUsers) {
        mInflater = LayoutInflater.from(context);
    }

    void setUsers(List<User> users) {
        mUsers = users;
        notifyDataSetChanged();
    }

    class UserViewHolder extends RecyclerView.ViewHolder {
        private TextView userListItem;

        public UserViewHolder(View itemView) {
            super(itemView);
            userListItem = itemView.findViewById(R.id.userListItem);
        }
    }

    @Override
    public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.recyclyerview_user_list_item, parent, false);
        return new UserViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(UserViewHolder holder, int position) {
        User current = mUsers.get(position);
        holder.userListItem.setText(current.getClientName());
    }

    // getItemCount() is called many times, and when it is first called,
    // mUsers has not been updated (means initially, it's null, and we can't return null).
    @Override
    public int getItemCount() {
        return mUsers.size();

    }
}

ViewModel

public class ViewModel extends AndroidViewModel {

    private UserDao mUserDao;
    public Repository mRepository;
    public LiveData<List<User>> mAllUsers;

    public ViewModel (Application application) {
        super(application);
        mRepository = new Repository(application);
        mAllUsers = mRepository.getAllUsers();
    }

    public LiveData<List<User>> getAllUsers() { return mAllUsers; }

    public void addUser(User user) { mRepository.addUser(user); }

}

存储库

public class Repository {

    private UserDao mUserDao;
    private LiveData<List<User>> mAllUsers;

    public Repository(Application application) {
        UserDatabase db = UserDatabase.buildDatabase(application);
        mUserDao = db.userDao();
        mAllUsers = mUserDao.getAllUsers();
    }

    public LiveData<List<User>> getAllUsers() {
        return mAllUsers;
    }

    public void addUser(User user) {
        new insertAsyncTask(mUserDao).execute(user);
    }

    private static class insertAsyncTask extends AsyncTask<User, Void, Void> {

        private UserDao mAsyncTaskDao;

        insertAsyncTask(UserDao dao) {
            mAsyncTaskDao = dao;
        }

        @Override
        protected Void doInBackground(final User... params) {
            mAsyncTaskDao.addUser(params[0]);
            return null;
        }
    }
}

列表项的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/userListItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_marginTop="300dp"
        android:text="HELLO"
        android:textColor="#000000"
        android:visibility="visible"
        tools:visibility="visible" />

</LinearLayout>

片段中用于recyclerview的XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="admin.AdminListFragment">

    <androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/admin_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:background="@android:color/darker_gray"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:listitem="@layout/recyclyerview_user_list_item" />

</androidx.constraintlayout.widget.ConstraintLayout>

UserDatabase

@Database(entities = {User.class, Admin.class}, version = 6, exportSchema = false)
public abstract class UserDatabase extends RoomDatabase {
    private static volatile UserDatabase INSTANCE;
    public abstract UserDao userDao();
    public static final String SP_NAME = "users";

    public static UserDatabase buildDatabase(final Context context) {
        if (INSTANCE == null) {
            synchronized (UserDatabase.class) {
                if (INSTANCE == null) {
                    INSTANCE = Room.databaseBuilder(context.getApplicationContext(), UserDatabase.class, "users").build();
                }
            }
        }
        return INSTANCE;
    }

    /**
     * Override the onOpen method to populate the database.
     * For this sample, we clear the database every time it is created or opened.
     *
     * If you want to populate the database only when the database is created for the 1st time,
     * override RoomDatabase.Callback()#onCreate
     */
    private static RoomDatabase.Callback sRoomDatabaseCallback = new RoomDatabase.Callback() {

        @Override
        public void onOpen(@NonNull SupportSQLiteDatabase db) {
            super.onOpen(db);
            // If you want to keep the data through app restarts,
            // comment out the following line.
            new PopulateDbAsync(INSTANCE).execute();
        }
    };

    /**
     * Populate the database in the background.
     * If you want to start with more words, just add them.
     */
    private static class PopulateDbAsync extends AsyncTask<Void, Void, Void> {

        private final UserDao mDao;

        PopulateDbAsync(UserDatabase db) {
            mDao = db.userDao();
        }

        @Override
        protected Void doInBackground(final Void... params) {
            // Start the app with a clean database every time.
            // Not needed if you only populate on creation.
            //mDao.deleteAll();

            User user = new User("Dickon", "d");
            mDao.addUser(user);
            user = new User("Pumpy", "d");
            mDao.addUser(user);
            return null;
        }
    }
}

UserDao

package database;

import java.util.List;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import User.User;
import admin.Admin;

@Dao
public interface UserDao {

    @Insert
    void addUser(User user);

    @Insert
    void createAdmin(Admin admin);

    @Query("select * from users")
    LiveData<List<User>> getUser();

    @Query
    ("SELECT * from users ORDER BY clientName")
    LiveData<List<User>> getAllUsers();

    @Query("select * FROM users WHERE id = :id")
    List<User> getUserById(int id);

    @Query("select * FROM users WHERE clientName = :clientName LIMIT 1")
    List<User> getUserByName(String clientName);

    @Delete
    void DeleteUser(User user);
}

1 个答案:

答案 0 :(得分:0)

您在AdminListActivity中拥有

   setContentView(R.layout.fragment_admin_list);

   RecyclerView recyclerView = findViewById(R.id.admin_recycler_view);
   final UserListAdapter adapter = new UserListAdapter(this, mUsers);
   recyclerView.setAdapter(adapter);
   recyclerView.setLayoutManager(new LinearLayoutManager(this));

在您的AdminListFragment中

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_admin_list, container, false);

    mUserRecyclerView = (RecyclerView) view.findViewById(R.id.admin_recycler_view);
    mUserRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    ...
}

片段中的recyclerView与活动中的recyclerView不同。因此,您可以在“活动”中看到用户,但在片段中看不到。 您的片段recyclerView没有适配器。