【问题标题】:Sending bulk of data from one activity to another activity?将大量数据从一个活动发送到另一个活动?
【发布时间】:2015-04-17 20:09:58
【问题描述】:

朋友们,我有 2 个文本框... 用户必须填写所有文本框,然后按提交按钮。 按下提交按钮后,新活动应该开始,所有填写的信息都应该显示在那里。 我已经尝试过使用意图,但第二个活动只显示一个文本框的内容。 请告诉我该怎么办? 提前致谢

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

}

}

这是我的第二个活动

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);
}

}

我只获取第二个文本字段(消息 2)的文本...

怎么办???

【问题讨论】:

  • 请发布您的代码。
  • 您需要使用尽可能少的源代码行创建一个简单的示例来演示您的问题。
  • 要么使用共享优先权,要么使用 intent.putextra() 方法发送字符串....并在第二次活动中使用 bundle 接收它
  • 您在第一个活动中使用相同的参考名称,例如两个文本框的 EXTRA_MESSAGE,只需重命名一个文本框,例如 EXTRA_MESSAGE1 以及第二个活动。它解决了您的问题。请参考下面的答案。

标签: android


【解决方案1】:

假设您有三个 ID 分别为 edt1、edt2、edt3 的 EditText 和一个提交按钮和两个活动,即所有三个标签和一个按钮所在的 MainActivity 和另一个活动,其中三个 TextView 的 ID 分别为 txt1、txt2、txt3 ,您必须在其中显示信息。

您可以通过以下方式使用意图将信息从一个活动发送到另一个活动:

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

其中 Key 是您可以从中发送数据的任何字符串,而 data 是您要发送的信息。请记住,此密钥需要在另一个活动中接收数据。

然后在 MainActivity 首先你需要找到视图然后在提交按钮点击监听器你需要做如下:

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);
        }
    });

然后在AnotherActivity中你需要找到你定义的所有TextViews的视图,然后获取MainActivity发送的数据并将其设置为textview你需要做如下:

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

记住密钥必须与发送数据时指定的相同。因此,最好将它们声明为资源字符串或类的public static final String 字段,而不是尝试手动保持它们同步。

【讨论】:

    【解决方案2】:

    试试这个来传递值。

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

    接收活动

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

    【讨论】:

      【解决方案3】:

      获取文本框的值并将其存储到一些变量例如 val1val2 然后使用

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

      在第二个Activity中写入

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

      【讨论】:

        【解决方案4】:

        您可以使用Intent.putExtra 方法来执行此操作。

        例如:

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

        并且在目标activity的OnResumeOnStart中:

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

        见:

        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)

        【讨论】:

          【解决方案5】:
          1. 使用putExtra()Intent,并使用来自不同文本框的不同键
          2. 在另一个 Activity 中,使用 intent.getExtras(),并通过您的密钥检索信息

          【讨论】:

            【解决方案6】:

            有一个有用的方法是共享偏好。通过它您可以从任何活动中创建、编辑、删除数据,也可以从任何活动中获取数据。

            从要存储数据的 Activity 创建或编辑共享首选项:

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

            从您想要数据的活动中获取数据:

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

            删除:

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

            【讨论】:

            • 但你必须了解偏好的意图。这不用于传递数据。这些用于存储数据。
            • 但这是一个很好的设计,可以存储来自任何活动的大量数据,并在应用程序的任何地方使用它(无论它是否是活动)。 Intent 只能用于一次从一个活动传递两个另一个活动的值。共享偏好是存储数据的最佳方式之一。 @M-WaJeEh
            • 并且问题所有者已经使用了意图,这对他使用另一种技术很有帮助。 @M-WaJeEh
            【解决方案7】:

            使用 ArrayList 将大量数据从一个活动发送到另一个活动的最佳方式之一。

            这是一个将完整的数组列表数据发送到其他活动的示例。

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

            希望对你有所帮助。

            【讨论】:

              【解决方案8】:
              Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
              intent.putExtra("FirstTxt", textbox1);
              intent.putExtra("SecondTxt", textbox2); 
              intent.putExtra("ThirdTxt", textbox3);
              StartActivity(intent); 
              

              【讨论】:

                【解决方案9】:
                and for receiving     ``                                                     extrasIntent in = getIntent();
                    String first= in.getExtras().getString("FirstTxt");
                    String second= in.getExtras().getString("SecondTxt");
                    String third= in.getExtras().getString("ThirdTxt");                         it worked for me
                

                【讨论】:

                  【解决方案10】:

                  其实很简单

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

                  i.putExtra("variable_name","variable_value");开始活动(i);

                  它会启动第二个活动,您可以使用 from bundle 找到相同的数据

                  捆绑附加服务 = getIntent().getExtras();

                  if (extras != null){ 字符串值 = extras.getString("variable_name"); }

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2019-07-31
                    • 2017-01-07
                    • 2012-04-15
                    • 2020-02-11
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2012-08-04
                    相关资源
                    最近更新 更多