2017-09-20 6 views
0

startActivityForResult()メソッドを使用して2つのアクティビティ間でデータを渡すアンドロイドチュートリアルに従っています。この例は完全に機能します。しかし、一度入力してメインアクティビティの値を渡し、再び新しい値を送信しようとすると、以前入力された値が置き換えられます。私が欲しいのは、私が入力し、消滅することなくメインアクティビティに渡されたすべての値を保持することです。誰でも私を導くことができますか?私はあなたの親切な助けに感謝します。startActivityForResult()メソッドを使用してAndroidアクティビティ間でデータを渡し、それらのデータを保持する

main.Xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_vertical" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/textViewMessage" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:text="Message" 
     android:textColor="#031241" 
     android:textSize="20dp" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:gravity="center_horizontal" 
     android:onClick="getMessage" 
     android:text="Get Message" /> 

</LinearLayout> 

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    TextView textViewMessage; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // get The reference of The textView 
     textViewMessage = (TextView) findViewById(R.id.textViewMessage); 
    } 

    // Method to handle the Click Event on GetMessage Button 
    public void getMessage(View V) { 
     // Create The Intent and Start The Activity to get The message 
     Intent intentGetMessage = new Intent(this, SecondActivity.class); 
     startActivityForResult(intentGetMessage, 2);// Activity is started with requestCode 2 
    } 

    // Call Back method to get the Message form other Activity 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     // check if the request code is same as what is passed here it is 2 
     if (requestCode == 2) { 
      if (null != data) { 
       // fetch the message String 
       String message = data.getStringExtra("MESSAGE"); 
       // Set the message string in textView 
       textViewMessage.setText("Message from second Activity: " + message); 
      } 
     } 


    } 
} 

second.Xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_vertical" 
    android:orientation="vertical" > 

    <EditText 
     android:id="@+id/editTextMessage" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:textColor="#000000" 
     android:textSize="20sp" 
     android:hint="Enter The Message" /> 

    <Button 
     android:layout_marginTop="20dp" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:text="Submit Message" 
     android:onClick="submitMessage" /> 

</LinearLayout> 

SecondActivity.java

public class SecondActivity extends AppCompatActivity { 

     EditText editTextMessage; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_second); 
      // Get the reference of Edit Text 
      editTextMessage=(EditText)findViewById(R.id.editTextMessage); 
     } 


     public void submitMessage(View V) 
     { 
      // get the Entered message 
      String message=editTextMessage.getText().toString(); 
      Intent intentMessage=new Intent(); 

      // put the message in Intent 
      intentMessage.putExtra("MESSAGE",message); 
      // Set The Result in Intent 
      setResult(2,intentMessage); 
      // finish The activity 
      finish(); 
     } 

} 
+0

その後、私は..私は – sumit

+0

@sumitに提案に感謝し、あなたの前のメッセージを保存しますあなたは何を言っているのか理解しています。私は、テキストビューでテーブルの行にこれを使用する必要があります。それは可能ですか? – Kash

答えて

2

使用この:あなたは以前の値を使用したい、これまで

String[] previousValue = new String[10]; // length of your array 
    int i = 0; 

// Call Back method to get the Message form other Activity 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     // check if the request code is same as what is passed here it is 2 
     if (requestCode == 2) { 
      if (null != data) { 
       // fetch the message String 
       String message = data.getStringExtra("MESSAGE"); 
       previousValue[i] = message; 
       i++; 
       // Set the message string in textView 
       textViewMessage.setText("Message from second Activity: " + message); 
      } 
     } 


    } 

はその後、ちょうど配列を反復処理//。

for(int i = 0; previousValue.length(); i++){ 
//displaying all the previous values 
Log.e("someTag","value : "+previousValue[i]); 
} 
+0

ありがとうAndroidには非常に新しいんだとあなたがこれを行う方法のフローを提供してくださいすることができ、いくつかの配列またはリスト – Kash

+0

はい、特定の行でインデックス値を使用することができます – sumit

関連する問題