我正在尝试在活动之间传递数据
我使用Intent在常规活动之间传递数据
考虑下面的代码::
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表示 ::
{EDIT} - 如果这可以根据答案--- Ambiguity,因为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);
答案 0 :(得分:61)
将数据从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");
答案 1 :(得分:7)
从您的AndroidTabRestaurantDescSearchListView
活动
Intent intent = new Intent(this,RatingDescriptionSearchActivity.class );
intent.putExtras( getIntent().getExtras() );
startActivity( intent );
然后从RatingDescriptionSearchActivity
活动中致电
getIntent().getStringExtra("key")
答案 2 :(得分: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);
答案 3 :(得分: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");
答案 4 :(得分:2)
主要活动
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);
}
}
答案 5 :(得分:1)
在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");
答案 6 :(得分:0)
我喜欢使用此简洁代码仅传递一个值:
var info = L.control();
info.onAdd = function (map) {
this._div = L.DomUtil.create('div', 'info');
this.update();
return this._div;
};
info.update = function (props) {
// console.log(props);
this._div.innerHTML = ''
+(props? '<b><center style="color:#838383; margin-top:10px;">'+props.nombre+'</center></b>': '')
+(props? '<br><b><center style="color:#838383">'+props.web+'</center></b>': '');
+(props? '<br><img src="'+props.pic+'"style="width:40px;height:40px;">': '');
this._div.innerHTML += '<br /><img src="link.png" style="background:rgba(254,198,41,0.8); margin-left:20px; margin-right:15px; margin-top:0px">';
};
info.addTo(map);
这使编写和易于理解的代码更加简单。