2

テーブル行の次の利用可能な ID (テーブルに行を挿入するときに自動的に作成される) を取得する方法はありますか?

より正確に言うと、リストビューを含むアクティビティがあり、それらの各アイテムは 2 番目のアクティビティを使用して追加されます。2 番目のアクティビティでアイテムの詳細を追加し終えたら、そのアイテムを parcelable オブジェクトに渡します (DaoGenerator が作成したホルダー クラスの 1 つに Parcelable Interface を実装しました)。そのオブジェクトの id 値を null にすることはできず、それを writeLong(id) で渡し、Parcelable メソッドで readLong() で受け取るため、現在のアイテムを既に挿入して id 値を自動生成する必要があります。データベース。私がやりたいことは、これらのIDを生成し(データベースにアイテムを挿入せずに)、そのアイテムを最初のアクティビティに渡し、ユーザーがリストからそれらすべてのアイテムを保存することを決定したときに、それらすべてをデータベースに追加します単一のトランザクション。

私が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

}

これは、アイテムを作成してリストに送信する方法です。

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

2 に答える 2

1

はい、それを行う手段があります。ORM を使用している場合、これは !@#% のように簡単です。たとえば、次のことを行う必要があります。

  • ID を生成するシーケンス名を取得します (手動で作成していなくても、常に存在します)。

  • たとえば、コードで SQL クエリを作成します。

    session.createSQLQuery( "SELECT nextval( 'mySequenceName' )");

  • 次に、クエリを実行して一意の ID を取得します。

これが役立つことを願っています。

乾杯

于 2013-06-24T17:05:12.500 に答える