【问题标题】:How to start another activity and start a method inside it?如何启动另一个活动并在其中启动一个方法?
【发布时间】:2016-11-08 14:36:02
【问题描述】:

一旦setOnClickListener 执行,我想启动另一个活动并将变量cn.getID() 传输给它。在其他活动中,我想立即启动方法findlocation 并给它cn.getID()

findLocation 方法还没有完成。这个想法是,一旦它获得其他活动按钮的ID,我可以在我的数据库中使用 sqllite 搜索它所属的地方,获取经度和纬度并告诉mapcontroller 将世界地图聚焦在这一点上。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.verladestellen);
    final DB_Verladestellen db = new DB_Verladestellen(this);
    List<DB_Place> placeList = db.getAllDBPlaces();
    final LinearLayout layout = (LinearLayout) findViewById(R.id.verladestellen_liste);

    for (final DB_Place cn : placeList) {

        final LinearLayout row = new LinearLayout(this);
        row.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        Button place = new Button(this);
        place.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        place.setText(cn.getName());
        place.setId(cn.getID());
        row.addView(place);
        row.setId(cn.getID());

        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) place.getLayoutParams();
        params.weight = 1.0f;
        place.setLayoutParams(params);

        place.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Here I want to call the method to start the other activity 
                //and transmit cn.getID(). 
                openMap(null); 
            }
        });

        layout.addView(row);
    }
}

//The method to start the other activity
public void openMap(View view) {
    Intent intent = new Intent(this, UI_MainActivity.class);
    startActivity(intent);
}

这是我想在新活动开始后立即执行的方法:

public void findLocation(View view){
    MapView map = (MapView) findViewById(R.id.map);
    IMapController mapController = map.getController();
    mapController.setZoom(17);
    GeoPoint myLocation = new GeoPoint(PLACEHOLDER X  , PLACEHOLDER Y);
    mapController.animateTo(myLocation);
}

编辑: @Murat K。经过一些编辑,这就是我现在的整个班级:

public class UI_Verladestellen extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.verladestellen);
    final DB_Verladestellen db = new DB_Verladestellen(this);
    List<DB_Place> placeList = db.getAllDBPlaces();
    final LinearLayout layout = (LinearLayout) findViewById(R.id.verladestellen_liste);

    for (final DB_Place cn : placeList) {

        final LinearLayout row = new LinearLayout(this);
        row.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        Button place = new Button(this);
        place.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        place.setText(cn.getName());
        place.setId(cn.getID());
        row.addView(place);
        row.setId(cn.getID());

        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) place.getLayoutParams();
        params.weight = 1.0f;
        place.setLayoutParams(params);

        place.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openMap(cn.getID());
            }
        });

        layout.addView(row);
    }
}

public void openMap(int view) {
    Intent intent = new Intent(UI_Verladestellen.this, UI_MainActivity.class);
    intent.putExtra("findLocation", 1);
    startActivity(intent);
}

}

这是我在 UI_MainActivity 中的 onCreate 方法的 getIntent:

int i = getIntent().getIntExtra("findlocation", 999);
    if(i == 1){
        findLocation(i);
    }

当我编辑到我之前的评论时,我看不到我的 Button-ID 是在哪里收到的。起初我以为我会是我的 ID,但那行不通,因为按钮 ID 可以是从 1 到 n 的每个数字。

【问题讨论】:

  • 在您的 openMap() 方法中,您没有将任何内容放入 Intent 中。你必须照原样使用我的答案。
  • @MuratK。我以为我确实按原样使用了您的答案。也许我误解了你。那么你能告诉我我错过了什么吗?对不起,我不是很有经验。

标签: java android android-intent osmdroid


【解决方案1】:

您可以通过Intent 来实现此目的,例如

 Intent i = new Intent(BaseActivity.this, YourSecondActivity.class);
 intent.putExtra("METHOD_TO_CALL", 1);
 startActivity(i);

并在您的 onCreate() 起始 Activity 方法中检查它。

@Override
  public void onCreate(Bundle savedInstanceState) {
    int i = getIntent().getIntExtra("METHOD_TO_CALL", 999);
    if(i == 1){
      callMethod(i);
    }

编辑:

//The method to start the other activity
public void openMap(View view) {
    Intent intent = new Intent(this, UI_MainActivity.class);
    intent.putExtra("METHOD_TO_CALL", 1); // the 1 is a example, put your ID here
    startActivity(intent);
}

【讨论】:

  • 有些事情不正常。这就是它的理解方式:;intent.putExtra("findLocation", 1)int i = getIntent().getIntExtra("findlocation", 999) != 999; 最后一行 Android Studio 告诉我“不兼容的类型。必需:Int,找到:Boolean”。
  • @Glave 哎呀,删除 != 检查。我原本打算创建一个 if 语句。
  • 好的,活动现在开始,但还是有问题。我的setOnClickListener 现在包含此代码:openMap(cn.getID());,但是当我单击按钮时,新活动告诉我i 是 999(该数字有特殊原因吗?)而不是 1,并且不调用方法。还。我现在看不到在新活动中收到我的按钮 ID 的位置。如果我 i == 1 它不能是我的按钮 ID。
  • @Glave 方法getIntExtra(String, DefaultValue) 就是这样构建的。如果找不到该值的键,它将返回一个默认值,在这种情况下为 999。您在哪里调用 getIntent 并且您是否正确编写了键?
  • 我最后编辑了我的帖子。我不确定是否理解您的问题,所以我现在将代码写入其中。也许您可以看到它有什么问题。
【解决方案2】:

第 1 步:使用 putExtra() 将您的 ID 值添加到与 startActivity() 一起使用的 Intent

步骤#2:在另一个活动中,在onCreate() 中调用getIntent() 以检索用于创建活动实例的Intent。调用get...Extra()(其中... 取决于数据类型)以检索您的ID 值。如果 ID 值存在,请调用您的 findLocation() 方法。

【讨论】:

    【解决方案3】:

    这取决于您希望它如何工作。如果您只希望它在活动创建时(开始或屏幕旋转时)执行,则在活动onCreate 方法中调用该方法。如果您希望在用户返回到该活动时调用它,其中可能包括他们离开应用程序并在稍后的某个时间返回它,那么onResume 将是一个更好的位置。不过,从任何一个调用该方法都应该像您希望的那样工作。

    我还建议查看 Activity 生命周期,因为这将对您未来有很大帮助。

    【讨论】:

      猜你喜欢
      • 2017-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多