java - 插入数据库时​​字符串转换奇怪

标签 java android sqlite android-room

我是一名学生,正在开发我的第一个 Android Studio 项目。我尝试搜索此问题导致出现大量日期格式结果,我有理由确信这不是问题所在。

我创建了一个带有 int 和三个 String 参数的对象。其中两个字符串是从 Date 对象转换而来的。一切似乎都很顺利,直到我调用 DAO 将其插入数据库。它确实插入了,但两个转换后的日期字符串不再是字符串。有人能告诉我为什么会发生这种情况以及如何阻止它吗?我对以下所需代码的最佳猜测:

@Override
protected Void doInBackground(final Void... params) {

    mTermDao.deleteAllTerms();

    Calendar calobj = Calendar.getInstance();
    calobj.add(Calendar.MONTH, -3);
    calobj.add(Calendar.DATE, -3);
    Date notToday = calobj.getTime();
    calobj.add(Calendar.MONTH, 6);
    Date next = calobj.getTime();
    DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
    String strDateOne = dateFormat.format(notToday);
    String strDateTwo = dateFormat.format(next);
    String termTitle = "Term 1";

    TermEntity termEntity= new TermEntity(1,termTitle, strDateOne, strDateTwo);
    mTermDao.insert(termEntity);

    return null;
}

与道

@Dao
public interface TermDAO {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insert(TermEntity termEntity);
}

View 模型

public class TermViewModel extends AndroidViewModel {
    private ScheduleRepository mRepository;
    private LiveData<List<TermEntity>> mAllTerms;
    public TermViewModel(Application application){
        super(application);
        mRepository=new ScheduleRepository(application);
        mAllTerms =mRepository.getAllTerms();
    }
    public LiveData<List<TermEntity>> getAllTerms(){
        return mAllTerms;
    }
    public void insert(TermEntity termEntity){
        mRepository.insert(termEntity);
    }
    public void delete(TermEntity termEntity){
        mRepository.delete(termEntity);
    }
    public int lastID(){
        return mAllTerms.getValue().size();
    }
}

术语实体

@Entity(tableName="term_table")
public class TermEntity {

    @PrimaryKey
    private int termId;
    private String termTitle;
    private String termStart;
    private String termEnd;

    public TermEntity(int termId, String termTitle, String termStart, String termEnd) {
        this.termId = termId;
        this.termTitle = termTitle;
        this.termStart = termStart;
        this.termEnd = termEnd;
    }

    @Override
    public String toString() {
        return "TermEntity{" +
                "termId=" + termId +
                ", termTitle='" + termTitle + '\'' +
                ", termStart=" + termStart +
                ", termEnd=" + termEnd +
                '}';
    }

    public int getTermId() {
        return termId;
    }

    public void setTermId(int termId) {
        this.termId = termId;
    }

    public String getTermTitle() {
        return termTitle;
    }

    public void setTermTitle(String termTitle) {
        this.termTitle = termTitle;
    }

    public String getTermStart() {
        return termStart;
    }

    public void setTermStart(String termStart) {
        this.termStart = termStart;
    }

    public String getTermEnd() {
        return termEnd;
    }

    public void setTermEnd(String termEnd) {
        this.termEnd = termEnd;
    }
}

访问对象的TermAdapter。

public class TermAdapter extends RecyclerView.Adapter<TermAdapter.TermViewHolder> {

    class TermViewHolder extends RecyclerView.ViewHolder {
        private final TextView termItemView;


        private TermViewHolder(View itemView) {
            super(itemView);
            termItemView = itemView.findViewById(R.id.termTextView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    final TermEntity current = mTerms.get(position);
                    Intent intent = new Intent(context, TermDetailsActivity.class);
                    intent.putExtra("termTitle", current.getTermTitle());
                    intent.putExtra("startTerm", current.getTermStart());
                    intent.putExtra("endTerm", current.getTermEnd());
                    intent.putExtra("termId", current.getTermId());
                    intent.putExtra("position",position);
                    context.startActivity(intent);
                }
            });
        }
    }

从获取 Intent 的 Activity

        if(getIntent().getStringExtra("termTitle")!=null) {
            String start = getIntent().getStringExtra("termStart");
            nameView.setText(getIntent().getStringExtra("termTitle"));
            startView.setText(start);
            endView.setText(getIntent().getStringExtra("termEnd"));
        } 

它确实创建了一个对象并插入到数据库中,但后来当我使用开始和结束字符串的 getter 函数来填充 textView 时,它们显示为 null。程序没有错误,只是字段显示为空白。

我应该补充一下,我的理论是基于在第一个代码块的最后两行上运行带断点的调试。在这一行: TermEntity termEntity= new TermEntity(1,termTitle, strDateOne, strDateTwo); 我的 IDE 将 strDateOne 和 strDateTwo 的值显示为字符串。 然而,当它运行下一行时,它们没有像“Term 1”那样被单引号包围,而是没有引号。

最佳答案

您只是使用了错误的Intent bundle 名称。 “startTerm”、“termStart”和 end 等

为了避免这种情况,请在发送 Activity 中使用常量字符串INTENT_CONST,并在接收 Activity 中使用它,例如intent.getExtras(SendingActivity.INTENT_CONST, value);

关于java - 插入数据库时​​字符串转换奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61599441/

相关文章:

java - 数据库重启后尝试重新连接 jdbc 池数据源

java - 在 Uno 中获取 for 循环以反转顺序

android - 根据我的需要选择正确的数据库

android - whereArgs SQLite 数据库删除

java - 程序中得到意外的输出

java - 如何将图标转换为图像

android - Jersey/Rest 默认字符编码

android - 谷歌地图V2的亮度很低

.net - 验证sqlite数据库中的数据未被其他人修改(即对数据签名)

ios - 将字符串转换为数组字符