房间 - SELECT 没有 return 任何数据

Room - SELECT doesn't return any data

无法找到我的错误所在 - 似乎插入了数据(我通过设备文件资源管理器检查了数据库文件)但没有 returns。 我想知道它是在 Adapter 还是 ViewHolder 或其他任何地方。 任何帮助都会被授予!

这是我执行查询的 activity

public class ShowDatabaseActivity extends AppCompatActivity {
    private List <Contact> contactsList = new ArrayList<>()

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_database);
        setupToolbar();
        initRecyclerView();
        Intent intent = getIntent();
        unpack(intent);
    }

    private void setupToolbar() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
            toolbar.setNavigationOnClickListener(v -> onBackPressed());
        }
    }

    private void initRecyclerView() {
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        final ContactsListAdapter adapter = new ContactsListAdapter(contactsList);
        adapter.notifyDataSetChanged();
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter);
    }

    private void unpack(Intent intent) {
        final Handler handler = new Handler();
        Thread backgroundThread = new Thread(() -> {
            Bundle extras = intent.getExtras();
            String lastName = extras.getString(Constants.LAST_NAME_KEY);
            String firstName = extras.getString(Constants.FIRST_NAME_KEY);
            String middleName = extras.getString(Constants.MIDDLE_NAME_KEY);
            int age = extras.getInt(Constants.AGE_KEY);
            Contact contact = new Contact(lastName, firstName, middleName, age);
            AppDatabase.getINSTANCE(ShowDatabaseActivity.this).contactDao().insert(contact);
            AppDatabase.getINSTANCE(ShowDatabaseActivity.this).contactDao().getAll();
            handler.post(() -> {

            });
        });
        backgroundThread.start();
    }
}

如果我调试这条线成功显示数据 -

(ShowDatabaseActivity.this).contactDao().insert(contact);

我的适配器

public class ContactsListAdapter extends RecyclerView.Adapter<ContactsListAdapter.ContactViewHolder> {
    private Context context;
    private List<Contact> contacts;

    public ContactsListAdapter(@NonNull List<Contact> contacts) {
        this.contacts = contacts;
    }

    @Override
    public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final LayoutInflater inflater = LayoutInflater.from(context);
        View itemView = inflater.inflate(R.layout.recycler_view, parent, false);
        return new ContactViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ContactViewHolder holder, int position) {
            Contact currentContact = contacts.get(position);
            if (currentContact!=null) {
                holder.contactLastNameView.setText(currentContact.getLastName());
                holder.contactFirstNameView.setText(currentContact.getFirstName());
                holder.contactMiddleNameView.setText(currentContact.getMiddleName());
                holder.contactAgeView.setText(Integer.toString(currentContact.getAge()));
            }
            else {
                holder.contactLastNameView.setText("No information");
                holder.contactFirstNameView.setText("No information");
                holder.contactMiddleNameView.setText("No information");
                holder.contactAgeView.setText("No information");
            }
    }

    @Override
    public int getItemCount() {
        return contacts.size();
    }

    class ContactViewHolder extends RecyclerView.ViewHolder {
        private final TextView contactLastNameView;
        private final TextView contactFirstNameView;
        private final TextView contactMiddleNameView;
        private final TextView contactAgeView;

        private ContactViewHolder(View itemView) {
            super(itemView);
            contactLastNameView = itemView.findViewById(R.id.last_name_text_view);
            contactFirstNameView = itemView.findViewById(R.id.first_name_text_view);
            contactMiddleNameView = itemView.findViewById(R.id.middle_name_text_view);
            contactAgeView = itemView.findViewById(R.id.age_text_view);
        }
    }

    @Override
    public int getItemViewType(final int position) {
        return R.layout.recycler_view;
    }
}

我的数据库

@Database(entities = {Contact.class}, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
    public abstract ContactDao contactDao();

    private List<Contact> allContacts;

    List<Contact> getAllContacts() {
        return allContacts;
    }

    private static AppDatabase INSTANCE;

    public synchronized static AppDatabase getINSTANCE(Context context) {
            INSTANCE = getDatabase(context);
        return INSTANCE;
    }

    private static AppDatabase getDatabase(final Context context) {
        if (INSTANCE == null) {
            synchronized (AppDatabase.class) {
                    INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                            AppDatabase.class, "table_contacts")
                            .build();
                Log.d("LOG", "Getting the database instance");
            }
        }
        return INSTANCE;
    }
}

毫无疑问,我的 Entity 和 Dao 类 因为它与其他数据库变体完美配合,所以我没有附加它们。 非常感谢任何帮助!

 AppDatabase.getINSTANCE(ShowDatabaseActivity.this).contactDao().getAll();

此代码不会将数据分配给任何东西/变量来保存您的数据。将数据分配给引用变量并将该变量传递给适配器,以便它可以显示来自它的数据。

您的代码不完整

您在适配器中仅设置了空数组,要纠正此问题,请将适配器的对象设为全局

你的handler.post里面什么都没有

您需要在适配器内部创建一个函数,如下所示

      public void addItem(List<Contacts> list) {

        mList.add(list) 

 notifyItemInserted(mList.size - 1)

    }

现在您需要使用

handler.post 中调用 addItem

adapter.addItem(contact)

这会将内容添加到您的适配器列表中并通知更改