会议室未将数据插入表格

时间:2018-08-11 15:53:42

标签: java android android-room android-architecture-components android-livedata

有趣的一点是,有时它会正确执行插入操作。我不知道为什么以及如何发生这种情况。因此,我不知道自己在哪里犯了错误。 这是我的项目文件。

1)SentFilesDao.java

@Dao
public interface SentFilesDao {
    @Query("SELECT id, name, type, status, dateTime FROM sent")
    LiveData<List<SentFilesEntity>> getAllSentFiles();

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    void insert(SentFilesEntity file);

    @Query("DELETE FROM sent")
    void deleteAllRecords();
}

2)SentFilesRepository.java

public class SentFilesRepository {
    private SentFilesDao mSentFilesDao;
    private LiveData<List<SentFilesEntity>> mAllSentFiles;

    SentFilesRepository(Application application) {
        AppDatabase db = AppDatabase.getDatabase(application);
        mSentFilesDao = db.mSentFilesDao();
        mAllSentFiles = mSentFilesDao.getAllSentFiles();
    }

    LiveData<List<SentFilesEntity>> getAllSentFiles() { return mAllSentFiles; }

    public void insert(SentFilesEntity sentFilesEntity) {
        new SentFilesRepository.insertAsyncTask(mSentFilesDao).execute(sentFilesEntity);
    }

    private static class insertAsyncTask extends AsyncTask<SentFilesEntity, Void, Void> {
        private SentFilesDao mAsyncTaskDao;

        insertAsyncTask(SentFilesDao dao) {
            mAsyncTaskDao = dao;
        }

        @Override
        protected Void doInBackground(final SentFilesEntity... params) {
            mAsyncTaskDao.insert(params[0]);
            Log.e("doInBackground: ", "reached here!"); // no any log in Logcat about this. Weird...
            return null;
        }
    }
}

3)SentFilesEntity.java

@Entity(tableName = "sent")
public class SentFilesEntity {
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    public int s_f_id;

    @ColumnInfo(name = "name")
    public String s_f_name;

    @ColumnInfo(name = "type")
    public String s_f_type;

    @ColumnInfo(name = "status")
    public String s_f_operation_status;

    @ColumnInfo(name = "dateTime")
    public String s_f_time;
}

4)SentFilesViewModel.java

public class SentFilesViewModel extends AndroidViewModel {
    private SentFilesRepository mRepository;
    private LiveData<List<SentFilesEntity>> mAllSentFiles;

    public SentFilesViewModel(Application application) {
        super(application);
        mRepository = new SentFilesRepository(application);
        mAllSentFiles = mRepository.getAllSentFiles();
    }

    public LiveData<List<SentFilesEntity>> getAllSentFiles() { return mAllSentFiles; }

    public void insert(SentFilesEntity sentFile) { mRepository.insert(sentFile); }
}

我要做什么:

我的程序通过WiFi Direct将所选文件从一台设备发送到另一台设备。在接收或发送文件时,我想保存一些有关文件的信息(例如文件名,文件类型(图像,视频,音乐,APK等),发送/接收日期和时间)以及操作(成功或失败)。我能处理所有这些事情。

出了什么问题?

好吧,获取所有属性(我上面提到的那些属性)并不是 end 。主要问题是将数据保存到数据库。为此,我创建了一些必需的文件(我在上面共享了它们的代码)。发送文件时,我正在尝试将相关信息插入数据库。但是它不会插入它。那是我的问题。

好吧,主要代码太长了,无法在此处发布。但是,我将最新代码发布到了GitHub项目和主要逻辑starts from here上,并将其连接到FileStatisticsActivity

p.s:也许共享项目链接不是道德,对此我感到非常抱歉。

谢谢。

0 个答案:

没有答案