TazHinkle
TazHinkle

Reputation: 58

Strings converting oddly when inserted into database

I'm a student working on my first Android Studio project. My attempts to search for this problem have led to a lot of date formatting results which I'm reasonably sure is not the problem.

I create an object with an int and three String parameters. Two of the strings are converted from Date object. It all seems to be going well until I call the DAO to insert it into the database. It does insert, but the two converted-date strings are no longer strings. Can someone tell me why this is happening and how I can make it stop? My best guesses at the needed code below:

@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;
}

And the Dao

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

ViewModel

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();
    }
}

The TermEntity

@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 where the object is accessed.

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);
                }
            });
        }
    }

From the Activity where the intent is picked up

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

It does create an object and inserts into the database, but later when I use the getter function for the start and end strings to populate a textView, they show up as null. The program doesn't error, the fields just appear blank.

I should add my theory is based on running debug w/breakpoints on the last two lines of the first code block. On this line: TermEntity termEntity= new TermEntity(1,termTitle, strDateOne, strDateTwo); my IDE showed the values for strDateOne and strDateTwo as strings. When it ran the next line however, instead of being surrounded by single quotes like 'Term 1', they did not have quotes.

Upvotes: 0

Views: 52

Answers (1)

Stanislav Batura
Stanislav Batura

Reputation: 420

You just use wrong intent bundle names. "startTerm", "termStart" and for end so on

To avoid this use constant string INTENT_CONST in sending activity and use it from a recieving activity like intent.getExtras(SendingActivity.INTENT_CONST, value);

Upvotes: 1

Related Questions