29

我正在尝试在活动之间传递数据

我使用意图在常规活动之间传递数据

考虑下面的代码::

AndroidTabRestaurantDescSearchListView.java

public class AndroidTabRestaurantDescSearchListView extends TabActivity {

    // TabSpec Names
    private static final String INBOX_SPEC = "Rating";
    private static final String OUTBOX_SPEC = "Price";

    Button Photos;
    Button Filter;
    Button Search;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost tabHost = getTabHost();

        // Inbox Tab
        TabSpec inboxSpec = tabHost.newTabSpec(INBOX_SPEC);
        Intent inboxIntent = new Intent(this, RatingDescriptionSearchActivity.class);
        inboxSpec.setIndicator(INBOX_SPEC);
        // Tab Content
        inboxSpec.setContent(inboxIntent);

        // Outbox Tab
        TabSpec PriceSpec = tabHost.newTabSpec(OUTBOX_SPEC);
        Intent PriceIntent = new Intent(this, PriceDescriptionSearchActivity.class);
        PriceSpec .setIndicator(OUTBOX_SPEC);
        PriceSpec.setContent(PriceIntent);


        // Adding all TabSpec to TabHost
        tabHost.addTab(inboxSpec); 
        tabHost.addTab(PriceSpec); 

        //Set the current value tab to default first tab
        tabHost.setCurrentTab(0);

    }

}

假设我从称为意图Activity-1的其他活动发送数据AndroidTabRestaurantDescSearchListView

现在我怎样才能接收AndroidTabRestaurantDescSearchListView我从中获取的数据Activity-1,然后再将其传递给RatingDescriptionSearchActivity

图示为::

在此处输入图像描述

{EDIT} ——如果这可能基于答案——歧义,因为 AndroidTabRestaurantDescSearchListView 是一个选项卡活动

TabSpec inboxSpec = tabHost.newTabSpec(INBOX_SPEC);
        Intent inboxIntent = new Intent(this, RatingDescriptionActivity.class);
        intent.putExtra("keyName", value);
        inboxSpec.setIndicator(INBOX_SPEC);
        // Tab Content
        inboxSpec.setContent(inboxIntent);
4

7 回答 7

64

将数据从Activity-1to传递AndroidTabRes..如下:

在发送活动...

Intent intent = new Intent(current.this, AndroidTabRestaurantDescSearchListView.class);
intent.putExtra("keyName","value");
startActivity(intent);

AndroidTabRes..活动...

  String data = getIntent().getExtras().getString("keyName");

因此,您可以在从发送活动接收活动时获得数据......

在你的AndroidTabRestaurantDescSearchListView课堂上,这样做:

String value= getIntent().getStringExtra("keyName");

Intent intent = new Intent(this, RatingDescriptionSearchActivity.class);
intent.putExtra("keyName", value);
startActivity(intent);

然后在你的RatingDescriptionSearchActivity课堂上,这样做:

 String data= getIntent().getStringExtra("keyName");
于 2013-10-10T04:28:33.477 回答
7

AndroidTabRestaurantDescSearchListView从你的活动 中试试这个

Intent intent  = new Intent(this,RatingDescriptionSearchActivity.class );
intent.putExtras( getIntent().getExtras() );
startActivity( intent );  

然后从RatingDescriptionSearchActivity活动中调用

getIntent().getStringExtra("key")
于 2013-10-10T04:30:48.803 回答
5

您可以使用 Bundle 获取数据:

Bundle extras = intent.getExtras();
String data = extras.getString("data"); // use your key 

再次,您可以将此数据传递给下一个活动:

 Intent intent = new Intent(this, next_Activity.class);
   intent.putExtra("data", data);
   startActivity(intent);
于 2013-10-10T04:33:23.453 回答
3

简单的。

假设在您的 中Activity-1,您这样做了:

String stringExtra = "Some string you want to pass";

Intent intent = new Intent(this, AndroidTabRestaurantDescSearchListView.class);

//include the string in your intent
intent.putExtra("string", stringExtra);

startActivity(intent);

在您的 AndroidTabRestaurantDescSearchListView 类中,执行以下操作:

//fetch the string  from the intent
String extraFromAct1 = getIntent().getStringExtra("string");

Intent intent = new Intent(this, RatingDescriptionSearchActivity.class);

//attach same string and send it with the intent
intent.putExtra("string", extraFromAct1);
startActivity(intent);

然后在您的 RatingDescriptionSearchActivity 类中,执行以下操作:

String extraFromAct1 = getIntent().getStringExtra("string");
于 2013-10-10T04:25:33.000 回答
3

主要活动

public class MainActivity extends Activity {

    EditText user, password;
    Button login;

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

        user = (EditText) findViewById(R.id.username_edit);
        password = (EditText) findViewById(R.id.edit_password);
        login = (Button) findViewById(R.id.btnSubmit);
        login.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,Second.class);

                String uservalue = user.getText().toString();
                String name_value = password.getText().toString();
                String password_value = password.getText().toString();

                intent.putExtra("username", uservalue);
                intent.putExtra("password", password_value);
                startActivity(intent);
            }
        });
    }
}

您要在其中接收数据的第二个活动

public class Second extends Activity{

    EditText name, pass;
    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);

        name = (EditText) findViewById(R.id.editText1);
        pass = (EditText) findViewById(R.id.editText2);

        String value = getIntent().getStringExtra("username");
        String pass_val = getIntent().getStringExtra("password");
        name.setText(value);
        pass.setText(pass_val);
    }
}
于 2015-05-11T11:34:23.300 回答
1

在第一个活动中:

Intent sendDataToSecondActivity = new Intent(FirstActivity.this, SecondActivity.class);
sendDataToSecondActivity.putExtra("USERNAME",userNameEditText.getText().toString());
sendDataToSecondActivity.putExtra("PASSWORD",passwordEditText.getText().toString());
startActivity(sendDataToSecondActivity);

在SecondActivity

onCreate()

String userName = getIntent().getStringExtra("USERNAME");
String passWord = getIntent().getStringExtra("PASSWORD");
于 2017-03-17T09:54:13.953 回答
0

我喜欢使用这个干净的代码只传递一个值:

startActivity(new Intent(context, YourActivity.class).putExtra("key","value"));

这使得编写和理解代码更简单。

于 2018-10-15T17:28:30.970 回答