0

やあ、私は Android プログラミングにかなり慣れていませんが、とにかく .net の経験があり、クラスを作成してRestartDialog、アクティビティからこのクラスを呼び出します。通常、.net では次のように使用します。

RestartDialog rd = new RestartDialog();

rd.setType(EXTENDED_TYPE);
rd.show;

その後、拡張モードで開始しますが、Android ではアクティビティを開始するためにインテントが必要です。これが唯一の方法です。etcを使用できることはわかっていIntent.putExtraますが、最初に多くの値を設定する必要があります。

これを達成するための最善の策は何でしょうか? よろしくお願いします。

4

3 に答える 3

1

インテントは、データを送信する方法です。したがって、多くのデータを送信する必要がある場合は、を使用できますParcelable。また、はるかに高速です..

オブジェクトを渡すだけの場合はParcelable、このために設計されています。これを使用するには、Java のネイティブ シリアル化を使用するよりも少し手間がかかりますが、はるかに高速です (つまり、はるかに高速です)。

ドキュメントから、実装方法の簡単な例は次のとおりです。

// simple class that just has one member property as an example
public class MyParcelable implements Parcelable {
    private int mData;

    /* everything below here is for implementing Parcelable */

    // 99.9% of the time you can just ignore this
    public int describeContents() {
        return 0;
    }

    // write your object's data to the passed-in Parcel
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(mData);
    }

    // this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
    public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() {
        public MyParcelable createFromParcel(Parcel in) {
            return new MyParcelable(in);
        }

        public MyParcelable[] newArray(int size) {
            return new MyParcelable[size];
        }
    };

    // example constructor that takes a Parcel and gives you an object populated with it's values
    private MyParcelable(Parcel in) {
        mData = in.readInt();
    }
}

Observe that in the case you have more than one field to retrieve from a given Parcel, you must do this in the same order you put them in (that is, in a FIFO approach).

Once you have your objects implement Parcelable it's just a matter of putting them into your Intents with putExtra():
Intent i = new Intent();
i.putExtra("name_of_extra", myParcelableObject);

Then you can pull them back out with getParcelableExtra():
Intent i = getIntent();
MyParcelable myParcelableObject = (MyParcelable) i.getParcelableExtra("name_of_extra");

GSON を使用してデータを送信することもできます。

于 2013-08-06T04:04:48.677 に答える
1

まず、以下を作成しますIntent

Intent intent = new Intent();

インテントをデータ値を保存する方法と考えてください。

intent.putExtra("type", EXTENDED_TYPE);

インテントに情報を入れ終わったら、アクティビティを開始します。

startActivity(intent);

次に、新しいアクティビティで、onCreate メソッドで必要な値を抽出します。

...
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.email_login_activity);

    Intent intent = getIntent();
    this.type = intent.getIntExtra("type", 0);

0この場合、追加の「タイプ」が設定されていない場合にgetIntExtra が返されるようにしました。

他にご不明な点がございましたら、お問い合わせください。

于 2013-08-06T04:08:27.837 に答える
0

最も簡単な解決策は次のとおりです。

ゲッター セッターを持つ静的データ メンバーを持つクラスを作成するには。

オブジェクトをあるアクティビティから設定し、別のアクティビティから取得します。

アクティビティ mytestclass.staticfunctionSet("","",""..etc.);

アクティビティ b mytestclass obj= mytestclass.staticfunctionGet();

于 2013-08-06T04:09:03.837 に答える