简体   繁体   English

尝试从第一类活动中调用方法,并在第二类活动中保留相同的参数(android studio)

[英]Trying to call a method from 1st class activity and keep the same argument in a 2nd class activity (android studio)

Currently finishing up an app I have been working on but I'm kinda stuck on this last thing. 目前正在整理我一直在研究的应用程序,但我对最后一件事有些犹豫。

The app has 2 activities, one with buttons that are categories and the other shows the information according to the button pressed. 该应用程序有2个活动,一个活动的按钮属于类别,另一个活动根据所按下的按钮显示信息。 For example if you click the button Fast food, it goes to the 2nd screen and displays info on that. 例如,如果您单击“快餐”按钮,它将转到第二个屏幕并显示有关此信息。

I'm trying to get a refresh button on the 2nd activity that will call a method in the 1st activity to refresh new information depending on the button pressed. 我正在尝试在第二个活动上获得一个刷新按钮,该按钮将在第一个活动中调用一个方法来刷新新信息,具体取决于所按下的按钮。 The problem is that I don't know how to make it so the method keeps the same argument when called. 问题是我不知道如何做到这一点,因此该方法在调用时保持相同的参数。 What I mean is, if fast food was clicked, the refresh button would get new info that still relates to the fast food category. 我的意思是,如果单击快餐,那么刷新按钮将获得仍与快餐类别有关的新信息。

Here's the relevant code in the Main activity: 这是Main活动中的相关代码:

public void yelpSearch(View view) {
    switch (view.getId()) {
        case R.id.button: buttoncategory = "Restaurant";
        break;
        case R.id.button2: buttoncategory = "Chinese restaurant";
        break;
        case R.id.button3: buttoncategory = "Fast Food";
        break;

and this on the 2nd Activity 这是第二个活动

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.refresh:
        Refresh();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

public void Refresh() {
MainActivity mActivity = new MainActivity();
mActivity.yelpSearch();
}

I'm not sure what to put inside mActivity.yelpSearch(); 我不确定要在mActivity.yelpSearch()中放入什么; I tried using (View view) but it'll say cannot resolve symbol view. 我尝试使用(视图视图),但是会说无法解析符号视图。 And if I make a local variable for view, it'll say not initialized and I don't know what to set it as 而且,如果我为视图创建局部变量,它将说未初始化,并且我不知道将其设置为

Any help would be awesome, been googling on this for a while now and searched through tons of questions on here as well. 任何帮助都会很棒,已经在该问题上搜索了一段时间,并在此处搜索了大量问题。 I'm still new at this so bear with me 我对此还很陌生,所以请多多包涵

I think you want to pass data between activities? 我想您想在活动之间传递数据吗?

First Activity(Send DATA): 首次活动(发送数据):

String data="YourXXXdata" 
Intent intent = new Intent(getBaseContext(), MainActivity.class);
intent.putExtra("DATANAME", data);
startActivity(intent)

second Activity(Receive DATA): 第二个活动(接收数据):

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("DATANAME");
}

The data now is in String value 现在的数据为String value

*you have to put in data the value you need depend you preview select button. *您必须输入数据所需的值取决于预览选择按钮。

Make your yelpSearch method static 使您的yelpSearch方法static
and call it like this from 2nd activity MainActivity.yelpSearch(); 并从第二个活动MainActivity.yelpSearch();这样调用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Android中的第一个应用程序开始第二个应用程序的活动 - Start Activity of 2nd app from 1st app in android 如何通过从第1类调用第2类的方法来调用第3类的方法 - How to call a method of a 3rd class by calling the method of the 2nd class from the 1st class 如何在 class 中将方法从第一种方法调用到第二种方法 - How to call method from 1st method to 2nd method in a class 如何从第二活动到第一活动获取数据 - How to get data from 2nd Activity to 1st Activity 使用android中的putextra和getextra方法从第二个活动转到第一个活动时图像视图的可见性消失了 - visibility gone of image view when come from 2nd activity to 1st activity using putextra and getextra method in android 从第一个活动拨打电话后,第二个活动没有通话 - 2nd Activity doesn't call after calling from 1st Activity Android - 在第一个活动中使用第二个活动的字符串列表 - Android - Use String from 2nd Activities for List on 1st activity 如何将代码从第二类添加到第一类 - How to add code from 2nd class to 1st class 从第一个活动的 Edittext 发送字符串到第二个活动的 Edittext - Send String from Edittext on 1st Activity to the Edittext on the 2nd Activity 当我返回到第一个活动时,第二个活动未调用第二个活动的onCreate()函数 - When I return back to 1st Activity the onCreate() function of 2nd Activity is not called in 2nd time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM