我有一个小问题:我有 3 个活动。活动列表类.java
public class ListClass extends Activity {
......
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
onItemClick1(parent, view, position, id);
}
});
}
public void onItemClick1(AdapterView<?> parent, View view, int position,
long id) {
String name = (String) parent.getItemAtPosition(position);
String address = (String) listAddress.get(position);
int status = (int) listStatus.get(position);
double lat = (double) listLat.get(position);
double lng = (double) listLng.get(position);
String serialNumber = (String) listSerialNumber.get(position);
getIntent().putExtra("name", name);
getIntent().putExtra("address", address);
getIntent().putExtra("status", status);
getIntent().putExtra("latitude", lat);
getIntent().putExtra("longitude", lng);
getIntent().putExtra("serialNumber", serialNumber);
startActivity(getIntent());
}
public Intent getIntent() {
Intent intent = new Intent(ListClass.this, ClassB.class);
return intent;
}
如您所见,我在 ClassB.java 上使用了额外的值:
public class ClassB extends Activity {
TextView textViewTitle;
TextView textView2;
TextView textViewInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.charge_box);
this.textViewTitle = (TextView) findViewById(R.id.charge_box__textView1);
this.textView2 = (TextView) findViewById(R.id.charge_box__textView2);
this.textViewInfo = (TextView) findViewById(R.id.charge_box__textView3);
getActionBar().setDisplayHomeAsUpEnabled(true);
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("name");
String address = bundle.getString("address");
int status = bundle.getInt("status");
textViewTitle.setText(name);
textViewInfo.setText(address + " " + status);
}
}
到目前为止,一切都很好!但我需要从 MainClass.java 的另一个活动启动 ActivityB。
Intent intent = new Intent(MainActivity.this,
ClassB.class);
startActivity(intent);
问题是当 ClassB 使用时,Bundle bundle = getIntent().getExtras();
他没有找到“额外”,因为 MainClass 的 Intent 与 ListClass 的 Intent 不同!我能怎么做??我可以决定使用哪个 Intent 吗?谢谢你!
编辑:@skygeek 好的,我正在处理它,但是如果额外的值发生变化,我该如何创建一个静态变量?
public void onItemClick1(AdapterView<?> parent, View view, int position,
long id) {
String name = (String) parent.getItemAtPosition(position);
String address = (String) listAddress.get(position);
int status = (int) listStatus.get(position);
double lat = (double) listLat.get(position);
double lng = (double) listLng.get(position);
String serialNumber = (String) listSerialNumber.get(position);
Intent intent = new Intent(ListClass.this, ChargeBoxInfoActivity.class);
intent.putExtra("name", name);
intent.putExtra("address", address);
intent.putExtra("status", status);
intent.putExtra("latitude", lat);
intent.putExtra("longitude", lng);
intent.putExtra("serialNumber", serialNumber);
startActivity(intent);
}