我在ModelView class.Debug
中遇到编译时错误。
我已经尝试了很多,但无法确定根本原因。
AddDataActivity class:
private void saveWord() {
String WordTitle=title.getText().toString().trim();
String Meaning=meaning.getText().toString().trim();
String Example= example.getText().toString().trim();
if (WordTitle.trim().isEmpty() || Meaning.trim().isEmpty() || Example.trim().isEmpty()) {
Toast.makeText(this, "plese insert title & description", Toast.LENGTH_LONG).show();
return;
}
Calendar calforDate= Calendar.getInstance();
SimpleDateFormat currentDateFoemat= new SimpleDateFormat("MMM dd,yyyy");
String date=currentDateFoemat.format(calforDate.getTime());
Calendar calForTime= Calendar.getInstance();
SimpleDateFormat currentTimeformat= new SimpleDateFormat("hh:mm a");
String time=currentTimeformat.format(calForTime.getTime());
WordTable wordTable=new WordTable(WordTitle,Meaning,Example,date,time);
wordViewModel.insert(wordTable);
Toast.makeText(this,"saved",Toast.LENGTH_SHORT).show();
}
WordTable类: ...
@Entity(tableName =“ word_table”) 公共类WordTable {
@PrimaryKey(autoGenerate = true)
private int id;
private String wordTitle;
private String wordMeaning;
private String wordExample;
private String date;
private String time;
public WordTable(String wordTitle, String wordMeaning, String wordExample, String date, String time) {
this.wordTitle = wordTitle;
this.wordMeaning = wordMeaning;
this.wordExample = wordExample;
this.date = date;
this.time = time;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public String getWordTitle() {
return wordTitle;
}
public String getWordMeaning() {
return wordMeaning;
}
public String getWordExample() {
return wordExample;
}
public String getDate() {
Calendar calforDate= Calendar.getInstance();
SimpleDateFormat currentDateFoemat= new SimpleDateFormat("MMM dd,yyyy");
date=currentDateFoemat.format(calforDate.getTime());
return date;
}
public String getTime() {
Calendar calForTime= Calendar.getInstance();
SimpleDateFormat currentTimeformat= new SimpleDateFormat("hh:mm a");
time=currentTimeformat.format(calForTime.getTime());
return time;
}
}
WordRepository类:
公共类WordRepository {
private WordTableDao wordTableDao;
private LiveData<List<WordTable>> getAllwords;
public WordRepository(Application application){
WordDatabase wordDatabase= WordDatabase.getInstance(application);
wordTableDao=wordDatabase.wordTableDao();
getAllwords=wordTableDao.getAllword();
}
public void insert(WordTable wordTable)
{
new insertAyncTask(wordTableDao).execute(wordTable);
}
public void update(WordTable wordTable)
{
new updateAyncTask(wordTableDao).execute(wordTable);
}
public void delete(WordTable wordTable)
{
new deleteAyncTask(wordTableDao).execute(wordTable);
}
public void deleteAllWord()
{
new deleteAllAyncTask(wordTableDao).execute();
}
public LiveData<List<WordTable>> getGetAllwords() {
return getAllwords;
}
public static class insertAyncTask extends AsyncTask<WordTable,Void,Void>{
private WordTableDao wordTableDao;
private insertAyncTask(WordTableDao wordTableDao)
{
this.wordTableDao=wordTableDao;
}
@Override
protected Void doInBackground(WordTable... wordTables) {
wordTableDao.insert(wordTables[0]);
return null;
}
}
public static class updateAyncTask extends AsyncTask<WordTable,Void,Void>{
private WordTableDao wordTableDao;
private updateAyncTask(WordTableDao wordTableDao)
{
this.wordTableDao=wordTableDao;
}
@Override
protected Void doInBackground(WordTable... wordTables) {
wordTableDao.update(wordTables[0]);
return null;
}
}
public static class deleteAyncTask extends AsyncTask<WordTable,Void,Void>{
private WordTableDao wordTableDao;
private deleteAyncTask(WordTableDao wordTableDao)
{
this.wordTableDao=wordTableDao;
}
@Override
protected Void doInBackground(WordTable... wordTables) {
wordTableDao.delete(wordTables[0]);
return null;
}
}
public static class deleteAllAyncTask extends AsyncTask<Void,Void,Void>{
private WordTableDao wordTableDao;
private deleteAllAyncTask(WordTableDao wordTableDao)
{
this.wordTableDao=wordTableDao;
}
@Override
protected Void doInBackground(Void... voids) {
wordTableDao.deleteAllword();
return null;
}
}
}
Vewimodel类:
公共类WordViewModel扩展了AndroidViewModel {
private WordRepository wordRepository;
private LiveData<List<WordTable>> Allwords;
public WordViewModel(@NonNull Application application) {
super(application);
wordRepository=new WordRepository(application);
Allwords=wordRepository.getGetAllwords();
}
public void insert(WordTable wordTable)
{
wordRepository.insert(wordTable);
}
public void update(WordTable wordTable)
{
wordRepository.update(wordTable);
}
public void delete(WordTable wordTable)
{
wordRepository.delete(wordTable);
}
public void deleteAll()
{
wordRepository.deleteAllWord();
}
public LiveData<List<WordTable>>getAllwords()
{
return Allwords;
}
}
请帮助我,我是android开发的初学者。
答案 0 :(得分:1)
那可能是因为您试图将Duplicate @primarykey值插入到房间中。请确保其值不相同。当您尝试更新相同的值时,它只会更新。