Is it any way for me to get the next available ID for a table row (that would be automatically created when inserting a row in the table), so I would not be forced to insert that row at a given time, to get it?
To be more precise: I have an activity which contains a listview and each of those items are added using a second activity. When I finish adding the item details in the second activity, I pass that item through a parcelable object (I implemented Parcelable Interface to one of the holders class that DaoGenerator created). The id value of that object can not be null, to pass it with writeLong(id), and receive it with readLong() in my Parcelable methods, so I have to auto-generate the id value, by inserting the current item already in the database.
What I would like to do is: generate those IDs (without inserting the item in the database), pass that item to first activity, and when user decides to save all those items from the list, I would add all of them to database in a single transaction.
some sample code I have atm:
public class Question implements Parcelable {
private Long id;
private String questionContent;
// KEEP FIELDS - put your custom fields here
// KEEP FIELDS END
public Question() {
}
public Question(Long id) {
this.id = id;
}
public Question(Long id,String questionContent) {
this.id = id;
this.questionContent = questionContent;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
// KEEP METHODS - put your custom methods here
// begin Parcelable implementation
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(id);
dest.writeString(questionContent);
}
public static final Parcelable.Creator<Question> CREATOR = new Parcelable.Creator<Question>() {
public Question createFromParcel(Parcel in) {
return new Question(in);
}
public Question[] newArray(int size) {
return new Question[size];
}
};
private Question(Parcel in) {
id = in.readLong();
questionContent = in.readString();
}
// end Parcelable implementation
// KEEP METHODS END
}
and this is how I create and send the item to list:
Question questionHolder = new Question(
null, etItemContent.getText().toString() .trim(),);
Log.d(LOG_TAG, "question id = "
+ questionHolder.getId());
// inserting it here, would auto-generate the ID I required,
// but I would like to do that to all of the items in the first Activity (containing the list of all of the items)
// questionDao.insert(questionHolder);
Log.d(LOG_TAG, "question id = "
+ questionHolder.getId());
// add item to intent
Bundle b = new Bundle();
b.putParcelable(IMPORTANCE_TAG, questionHolder);
Intent intent = new Intent();
intent.putExtras(b);
setResult(RESULT_OK, intent);
QuestionItemActivity.this.finish();