使用应用程序将数据从一项活动发送到另一项活动时出现问题

发布于 2024-12-11 11:23:21 字数 1532 浏览 0 评论 0原文

正如标题所说,我想传输数据,在本例中是用户在 EditTextSpinner 上引入的信息,从一个活动传输到另一个活动。

我正在遵循一本书中的教程,但它不起作用(我认为它不完整)。这是程序的代码:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等你爱我 2024-12-18 11:23:21

那么您的目标是将数据获取到 Restaurantactivity 吗?
通常,Android 中的数据通过使用 Intents 从一个 Activity 移交给另一个 Activity。

所以首先你要创建一个意图。
然后使用intent.putExtra()方法将要传输的数据放入intent中。
在获取意图的活动中,您可以使用 getIntent().getExtra() 方法获取数据(getExtra 可以类似于 getStringExtra())。

这是一个名为“name”的编辑框的小示例:

public void onCreate(Bundle iBundle){
  //do some stuff here
  //perhaps define some Buttos and so on

  //now lets start the activity
  Intent intent = new Intent(currentActivityname.this, ActivityYouWantToStart.class);
  intent.putExtra("name", name.getText().toString())
  startActivity(intent); // you can also start an startActivityForResult() here :)
}

在我们的接收活动中,您现在可以处理意图(例如在 onCreate() 方法中)

public void onCreate(Bundle iBundle){
String name = this.getIntent().getStringExtra("name",some default value);
}

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":

public void onCreate(Bundle iBundle){
  //do some stuff here
  //perhaps define some Buttos and so on

  //now lets start the activity
  Intent intent = new Intent(currentActivityname.this, ActivityYouWantToStart.class);
  intent.putExtra("name", name.getText().toString())
  startActivity(intent); // you can also start an startActivityForResult() here :)
}

In our receiving activity you can now handle the intent (e.g. in the onCreate() method

public void onCreate(Bundle iBundle){
String name = this.getIntent().getStringExtra("name",some default value);
}
朮生 2024-12-18 11:23:21

尝试将数据放入Bundle中并用此Bundle启动Activity

Intent intent = new Intent(this, YourSecondActivity.class);
intent.putExtra(... HERE YOUR BUNDLE WITH DATA ...);
startActivity(intent);

希望,它可以帮助你!

Try to put data in the Bundle and start Activity with this Bundle

Intent intent = new Intent(this, YourSecondActivity.class);
intent.putExtra(... HERE YOUR BUNDLE WITH DATA ...);
startActivity(intent);

Hope, it help you!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文