1

私のアプリには、オブジェクトのさまざまなフィールドを表す5つの文字列配列があります。

すなわち

String_A[1],
String_B[1],
String_C[1],
String_D[1],
String_E[1],

すべてが同じオブジェクトの属性です(実際にはオブジェクトではありません)。


今、私が作成している新しいアクティビティでそれらを使用できるように、それらを保存したいと思います。オブジェクトを渡すことができないので、共有設定に保存する必要があると思いました。

私の質問は、それらを別々の文字列として保存するか、それらすべてのフィールドを使用して新しいクラスを作成してから、オブジェクトをシリアル化する必要があるかどうかです。

メモリ使用量の観点から、どちらが最善の方法ですか?実際、同様の機能を実現する他の方法はありますか?

よろしくお願いしますマイク

4

3 に答える 3

2

それらの文字列配列のそれぞれが「十分」に大きく、それらを格納したいと思われる場合は、Sqliteを検討しましたか?SharedPreferencesは、プリミティブデータをキーと値のペアで格納するのに最も効果的です。このリンクを確認してください-あなたが持っているオプションについてのきちんとした比較があります-http://developer.android.com/guide/topics/data/data-storage.html

于 2011-02-06T19:35:25.850 に答える
0

インテントを介してオブジェクトを渡すことができます。インテントのextras関数は、バンドルを保存して指定されたアクティビティに送信できますが、いつでも呼び出すことはできません(明示的に送信されない後のアクティビティなど)。これが別のアクティビティへの1回限りのパスである場合は、おそらくそれを使用することをお勧めします。

http://developer.android.com/reference/android/content/Intent.html#putExtras%28android.content.Intent%29

しばらく前に作成したテストアプリの例を次に示します。

public void onClick(View v) {
            switch(v.getId()) { //this references the unique ID of the view that was clicked
                case R.id.Button01: //this is what happens when the Button in the XML with android:id="@+id/Button01" is clicked
            Intent nameGreet = new Intent(this, MainMenu.class);//creates Intent which will send the EditText input
                    String theName = firstName.getText().toString();// creates a new string named "theName" which is the text from an EditText called "firstName"
                    nameGreet.putExtra("helloName", theName);//puts the input from EditText into the Intent, this is a key/value pair
                    this.startActivity(nameGreet);//setting off the Intent
                    break;

次に、そのようにキャッチします。

@Override
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main_menu);
    String personsname = this.getIntent().getStringExtra("helloName");
    welcome = (TextView)this.findViewById(R.id.TextView01);
    welcome.setText(personsname);

お役に立てれば。

于 2011-02-06T19:36:46.067 に答える
0

Serializableを使用して渡すことができますIntent

Intent.putExtra(String name, Serializable value)

于 2011-02-06T20:35:15.097 に答える