无法获得我的错误在哪里-似乎已插入数据(我通过设备文件资源管理器检查了数据库文件),但没有返回。我想知道它是在Adapter还是ViewHolder还是其他地方。任何帮助都被授予!
这是我执行查询的活动
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
中调用addItemadapter.addItem(contact)
这将把内容添加到适配器的列表中,并同时通知频道