我想通过活动之间的数据
我使用意图通过经常性活动之间的数据
考虑下面的代码::
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
Pictoral表示是 ::
{编辑} -如果这是可能的基础上的答案---因为歧义是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);
Answer 1:
从数据传递Activity-1
至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");
Answer 2:
从你试试这个AndroidTabRestaurantDescSearchListView
活动
Intent intent = new Intent(this,RatingDescriptionSearchActivity.class );
intent.putExtras( getIntent().getExtras() );
startActivity( intent );
然后从RatingDescriptionSearchActivity
活动只是调用
getIntent().getStringExtra("key")
Answer 3:
您可以使用包获得的数据:
Bundle extras = intent.getExtras();
String data = extras.getString("data"); // use your key
而且你可以再次opass此数据下一个活动:
Intent intent = new Intent(this, next_Activity.class);
intent.putExtra("data", data);
startActivity(intent);
Answer 4:
简单。
假设在你的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");
Answer 5:
主要活动
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);
}
}
Answer 6:
在FirstActivity:
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");
Answer 7:
我喜欢用这个干净的代码通过一个仅值:
startActivity(new Intent(context, YourActivity.class).putExtra("key","value"));
这使得书写和理解的代码更简单。
文章来源: Using intents to pass data between activities