会议室数据库-首次不显示数据

时间:2020-10-24 11:42:35

标签: java database android-studio android-asynctask android-room

当我在Database类中填充数据时,我从MainActivity中的StudentDao调用了getAllStudents方法,但是当我运行该应用程序时,第一次我的textview不显示数据。当我第二次运行该应用程序时,仅这次是我的textview显示数据。问题是什么 ? 这是mycode:

学生班

@Entity(tableName = "students")
public class Student {
@PrimaryKey(autoGenerate = true)
private int ID;
private String name;
private String email;
private String phoneNumber;

public Student(String name, String email, String phoneNumber) {
    this.name = name;
    this.email = email;
    this.phoneNumber = phoneNumber;
}

public void setID(int ID) {
    this.ID = ID;
}

public int getID() {
    return ID;
}

public String getName() {
    return name;
}

public String getEmail() {
    return email;
}

public String getPhoneNumber() {
    return phoneNumber;
}

StudentDao

@Dao
public interface StudentDAO {
@Insert
void InsertSingleStudent(Student student);
@Insert
void InsertAllStudents(List<Student> students);
@Delete
void DeleteSingleStudent(Student student);
@Update
void UpdateSingleStudent(Student student);

数据库类:

@Database(entities = {Student.class},version =1 ,exportSchema = false)
public abstract  class UniDatabase extends RoomDatabase {
public abstract StudentDAO studentDAO();
private static UniDatabase instance;
public static synchronized  UniDatabase getInstance(Context context) {
    if (null==instance){
        instance=Room.databaseBuilder(context,UniDatabase.class,"uni_db")
                .fallbackToDestructiveMigration()
                .allowMainThreadQueries()
                .addCallback(initialCallback)
                .build();
    }

    new PopulateInitialData(instance).execute();
    return instance;
}
private static RoomDatabase.Callback initialCallback=new RoomDatabase.Callback() 
{
    @Override
    public void onCreate(@NonNull SupportSQLiteDatabase db) {
        super.onCreate(db);

    }
};
private static class PopulateInitialData extends AsyncTask<Void,Void,Void> {
    private StudentDAO studentDAO;
    public PopulateInitialData(UniDatabase db) {
        this.studentDAO=db.studentDAO();
    }
    @Override
    protected Void doInBackground(Void... voids) {
        ArrayList<Student> studentList=new ArrayList<>();
        studentList.add(new Student("Cemal","cemal@gmail","555"));
        studentList.add(new Student("Rıza","riza@gmail","123"));
        studentDAO.InsertAllStudents(studentList);
        return null;
    }
}

}

主要活动

 public class MainActivity extends AppCompatActivity {
 private TextView textView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = findViewById(R.id.textView);
    UniDatabase db = UniDatabase.getInstance(MainActivity.this);
    ArrayList<Student> students = (ArrayList<Student>) 
 db.studentDAO().getAllStudents();


    String text = "";
    for (Student s : students) {
        text += "Id: " + s.getID() + "\nName: " + s.getName() + "\nEmail: " + 
 s.getEmail();

    }
    textView.setText(text);

 }
 }

1 个答案:

答案 0 :(得分:0)

请参阅,如果您可以通过首先从设备或仿真器中删除数据库,然后运行代码来重新创建该问题。如果再次发生,则您的数据库创建有问题。

我认为,因为您的数据库正在AsyncTask中运行,所以它从不与您的Main线程协调以让其知道数据已准备就绪。再次运行该应用程序时,数据已经存在,因为AsyncTask在第一次运行时就完成了。