gpt4 book ai didi

java - 跨 Activity 发送整数显示 0

转载 作者:行者123 更新时间:2023-12-02 02:05:18 26 4
gpt4 key购买 nike

主要 Activity

            ...
...
locationsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

Log.i("ID SENT: ", toString().valueOf(i));
mapsIntent.putExtra("number", i );
startActivity( mapsIntent );
}
});

map Activity

        ...
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
Bundle bundle = intent.getExtras();
if ( bundle != null ) {

int id = getIntent().getIntExtra( "number", 0);
Log.i("LOCATION MARKED: ", id );
}

假设我点击了 ListView 上的项目 2,它在日志上显示“ID SENT: 2”,但在 MapsActivity 上它显示位置标记:0。在 ListView 上尝试了不同的项目,但仍然收到 0。

完整代码:

        Intent mapsIntent = getIntent();
Bundle bundle = mapsIntent.getExtras();
if ( bundle != null ) {

int id = mapsIntent.getIntExtra( "number", 0);
myLocations = (ArrayList<String>) bundle.getStringArrayList("locations");
vLoc1 = (ArrayList<String>) bundle.getStringArrayList("loc1");
vLoc2 = (ArrayList<String>) bundle.getStringArrayList("loc2");
Log.i("LOCATION MARKED: ", id + " " + myLocations.get( id ) + " " + vLoc1.get( id ) + " " + vLoc2.get( id ));

MainActivity(再次)是的

    @Override
protected void onPause() {
super.onPause();
if (myLocations.size() > 0 ) {
Intent intent = new Intent(getApplicationContext(), MapsActivity.class);
intent.putExtra("locations", myLocations);
intent.putExtra("loc1", vLoc1);
intent.putExtra("loc2", vLoc2);
startActivity(intent);
finish();
}
}

最佳答案

尝试:

声明全局 Intent mapIntent。

在主要 Activity 中:

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

Log.i("ID SENT: ", toString().valueOf(i));
mapsIntent = = new Intent(MainActivity.this, MapsActivity.class);
mapsIntent.putExtra("number", i );
startActivity( mapsIntent );
}

暂停方法:

@Override
protected void onPause() {
super.onPause();
if (myLocations.size() > 0 ) {
mapsIntent = new Intent(MainActivity.this, MapsActivity.class);
intent.putStringArrayListExtra("locations", myLocations);
intent.putStringArrayListExtra("loc1", vLoc1);
intent.putStringArrayListExtra("loc2", vLoc2);
startActivity(intent);
finish();
}
}

在 map Activity 中:

Intent mapsIntent = getIntent();
int id = mapsIntent.getIntExtra("number", 0);
myLocations = intent.getStringArrayListExtra("locations");

关于java - 跨 Activity 发送整数显示 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50926949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com