user2712154
user2712154

Reputation: 3

Sending bulk of data from one activity to another activity?

Friends i have 2 text boxes... user have to fill all the text boxes and then press the submit button. On pressing submit button new activity should start and all the filled information should be displayed over there. I have tried it using intent but the second activity showing only content of one textbox. please tell me what to do? Thanks in advance

package com.example.myfirstapp;

public class MainActivity extends Activity {

static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
public void sendMessage(View view)
{
    Intent intent = new Intent(this, DisplayMessageActivity.class); 

    EditText editText = (EditText) findViewById(R.id.edit_message);
    EditText editText1 = (EditText) findViewById(R.id.edit_message2);
    String message = editText.getText().toString();
    String message2 = editText1.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    intent.putExtra(EXTRA_MESSAGE, message2);
    startActivity(intent);

}

}

here is my second activity

package com.example.myfirstapp;

public class DisplayMessageActivity extends Activity {

enter code here

@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_display_message);

    // Get the message from the intent

    Intent intent = getIntent();

    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    // Set the text view as the activity layout
    setContentView(textView);
    // Show the Up button in the action bar.

   String message2 = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    TextView textView1 = new TextView(this);
    textView1.setTextSize(40);
    textView1.setText(message2);
    setContentView(textView1);
    setupActionBar();
}

/**
 * Set up the {@link android.app.ActionBar}, if the API is available.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.display_message, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

i am getting text of second text field (message 2) only...

what to do???

Upvotes: 0

Views: 2548

Answers (8)

Vivek  Samele
Vivek Samele

Reputation: 349

It is pretty simple

Intent i = new Intent(this, NewActivity.class);

i.putExtra("variable_name","variable_value"); startActivity(i);

It start the second activity and you can find the same data with from bundle

Bundle extras = getIntent().getExtras();

if (extras != null){ String value = extras.getString("variable_name"); }

Upvotes: 0

Aravin
Aravin

Reputation: 4108

Try this to pass the values.

Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", "textbox1");
i.putExtra("Value2", "textbox2"); 
i.putExtra("Value3", "textbox3");
StartActivity(i);

Receiving Activity

Intent in = getIntent();
String tv1= in.getExtras().getString("Value1");
String tv2= in.getExtras().getString("Value2");
String tv3= in.getExtras().getString("Value3");

Upvotes: 4

Mit
Mit

Reputation: 318

Suppose you have three EditText with id's edt1, edt2, edt3 respectively and a submit button and two activities namely MainActivity where all the three label and a button resides and another activity where there are three TextView with id's txt1, txt2, txt3 respectively, where you have to display the information.

You can send info from one activity to another using intent in the following way:

intent.putExtra("Key", "Data");

where Key is any string from which you can send the data and data is the info that you are sending. Remember this key is need to receive the data in another activity.

Then in the MainActivity first you need to find the views and then on submit button click listener you need to dos as follows:

btnSubmit.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent intent = new Intent(TestActivity.this, AnotherActivity.class);
            intent.putExtra("Name", edt1.getText().toString());
            intent.putExtra("Phone", edt2.getText().toString());
            intent.putExtra("Age", edt3,getText().toString())
            startActivity(intent);
        }
    });

Then in the AnotherActivity you need to find views for all the TextViews you defined and then to get the data send by MainActivity and set it to textview you need to do as follows:

txt1.setText(getIntent.getStringExtra("Name"));
txt2.setText(getIntent.getStringExtra("Phone"));
txt3.setText(getIntent.getStringExtra("Age"));

Remember the key must be the same as specified while sending the data. Therefore, declaring them as resource strings or public static final String fields of a class is best practice instead of trying to keep them in sync by hand.

Upvotes: 6

WU Xudong
WU Xudong

Reputation: 160

  1. use putExtra() of Intent, and use different keys from different textboxes
  2. in the other Activity, use intent.getExtras(), and retrieve info by your keys

Upvotes: 3

PrashantAdesara
PrashantAdesara

Reputation: 1917

One of the best way to send bulk of data using ArrayList from one activity to another activity.

Here's one example will send full arraylist data to other activity.

http://prashantandroid.blogspot.in/2013/08/passing-arraylist-from-one-activity-to.html

I hope this will help you.

Upvotes: 1

matthewrdev
matthewrdev

Reputation: 12200

You can use the Intent.putExtra method to do this.

For example:

Intent intent = new Intent(this, MyOtherActivity.class);
intent.putExtra("key", "value");
// ... etc

And in the OnResume orOnStart of the target activity:

Intent intent = getIntent();
String value = intent.getStringExtra("key");
// ... etc

See:

http://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String, java.lang.String)

http://developer.android.com/reference/android/content/Intent.html#getStringExtra(java.lang.String)

Upvotes: 3

Girish Thimmegowda
Girish Thimmegowda

Reputation: 2179

get the values of textbox and store it to some variables ex, val1 and val2 then use

Intent intent = new Intent(getBaseContext(), SecondActivity.class);
intent.putExtra("Textbox_val1", val1);
intent.putExtra("Textbox_val2", val2);
startActivity(intent)

In the second Activity write

Bundle extras = getIntent().getExtras(); 
if(extras !=null) {
String value1 = extras.getString(Textbox_val1);
String value2 = extras.getString(Textbox_val2);
}

Upvotes: 3

Avijit
Avijit

Reputation: 3824

There is an useful way is shared prefernce. Through which you can create,edit, delete data from any activity as well as can fetch data from any of the activity.

To create or edit shared preference from the activity where you want to store data:

String share_pref_file = "your_file_name";      
SharedPreferences prefs1 = getSharedPreferences(
        share_pref_time_file, Context.MODE_PRIVATE);
SharedPreferences.Editor editor1 = prefs1.edit();
editor1.putString("your_data", data); //data is your variable
editor1.commit();

To fetch data from the activity where you want the data:

String share_pref_file = "your_file_name";
SharedPreferences prefs = getSharedPreferences(share_pref_file,
    Context.MODE_PRIVATE);
String strJson = prefs.getString("your_data", "");

To delete:

String share_pref_file = "your_file_name";
SharedPreferences prefs1 = getSharedPreferences(
            share_pref_file, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs1.edit();
    editor.remove(share_pref_file);
    editor.clear();
    editor.commit();

Upvotes: 1

Related Questions