使用应用程序将数据从一项活动发送到另一项活动时出现问题
正如标题所说,我想传输数据,在本例中是用户在 EditText
和 Spinner
上引入的信息,从一个活动传输到另一个活动。
我正在遵循一本书中的教程,但它不起作用(我认为它不完整)。这是程序的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
this.location=(EditText)findViewById(R.id.location);
this.cuisine=(Spinner)findViewById(R.id.cuisine);
this.grabReviews=(Button)findViewById(R.id.get_reviews_button);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.cuisine, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
this.cuisine.setAdapter(adapter);
this.grabReviews.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
handleGetReviews();
}
}
);
}
private void handleGetReviews() {
RestaurantsActivity application= (RestaurantsActivity) getApplication();
application.setReviewCriteriaCuisine(this.cuisine.getSelectedItem().toString());
application.setReviewCriteriaLocation(this.location.getText().toString());
Intent intent=new Intent(Constants.INTENT_ACTION_VIEW_LIST);
startActivity(intent);
}
上面的代码不起作用。我不明白四件事:
-RestaurantsActivity
必须是实际活动,对吗?
- 在我在互联网上看到的所有示例中,都有一个应用程序扩展类,但在本示例中没有。
-setReviewCriteria
函数缺失
-Constants.INTENT_ACTION_VIEW_LIST
来自哪里?
As the title says I want to transfer data, in this case the information introduced by the user on an EditText
and a Spinner
, from one activity to another.
I am following a tutorial from a book but it doesn't work (I think its not complete). Here the code of the program:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
this.location=(EditText)findViewById(R.id.location);
this.cuisine=(Spinner)findViewById(R.id.cuisine);
this.grabReviews=(Button)findViewById(R.id.get_reviews_button);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.cuisine, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
this.cuisine.setAdapter(adapter);
this.grabReviews.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
handleGetReviews();
}
}
);
}
private void handleGetReviews() {
RestaurantsActivity application= (RestaurantsActivity) getApplication();
application.setReviewCriteriaCuisine(this.cuisine.getSelectedItem().toString());
application.setReviewCriteriaLocation(this.location.getText().toString());
Intent intent=new Intent(Constants.INTENT_ACTION_VIEW_LIST);
startActivity(intent);
}
This code above doesn't work. I dont understand four things:
-RestaurantsActivity
must be the actual activity right?
-In all the examples I have seen over the internet there is an application extends class, in this example there isnt.
-setReviewCriteria
function is missing
-Where does Constants.INTENT_ACTION_VIEW_LIST
come from ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么您的目标是将数据获取到 Restaurantactivity 吗?
通常,Android 中的数据通过使用 Intents 从一个 Activity 移交给另一个 Activity。
所以首先你要创建一个意图。
然后使用intent.putExtra()方法将要传输的数据放入intent中。
在获取意图的活动中,您可以使用 getIntent().getExtra() 方法获取数据(getExtra 可以类似于 getStringExtra())。
这是一个名为“name”的编辑框的小示例:
在我们的接收活动中,您现在可以处理意图(例如在 onCreate() 方法中)
So your target is to get the data to Restaurantsactivity?
Normally data in android are handed over from one activtiy to another by using Intents.
So first you create an intent.
Then you put the data you want to transfer into the intent by using the intent.putExtra() method.
In the activity that gets the intent you can get the data by using getIntent().getExtra() method (getExtra can be something like getStringExtra()).
Here is a small example for a edit box called "name":
In our receiving activity you can now handle the intent (e.g. in the onCreate() method
尝试将数据放入Bundle中并用此Bundle启动Activity
希望,它可以帮助你!
Try to put data in the Bundle and start Activity with this Bundle
Hope, it help you!