gpt4 book ai didi

java - 如何在可搜索字典示例中 putExtra()

转载 作者:行者123 更新时间:2023-12-01 16:04:30 26 4
gpt4 key购买 nike

基于 Searchable Dictionary示例 我尝试将额外的数据放入不同的 Activity 中。

 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Spinner distance = (Spinner) findViewById(R.id.distanceSpinner);
ArrayAdapter<CharSequence> adapterDistance = ArrayAdapter.createFromResource(
this, R.array.distance, android.R.layout.simple_spinner_item);
adapterDistance.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
distance.setAdapter(adapterDistance);
Intent intent = getIntent();

if (Intent.ACTION_VIEW.equals(intent.getAction())) {
// handles a click on a search suggestion; launches activity to show word
mapIntent = new Intent(this, Map.class);
mapIntent.setData(intent.getData());
mapIntent.putExtra("Distance", distance.getSelectedItemPosition());
startActivity(mapIntent);
finish();
}
}

在我的 map 类中,距离始终为零,因为 distance.getSelectedItemPostion() 获取初始化值。如何通过点击搜索建议来添加额外数据?谢谢

最佳答案

您必须注册距离微调器值更改的回调,并从那里调用启动 Map 类 Activity 的 Intent 。

private OnItemSelectedListener itemSelectedListener = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (parent.getId() == R.id.distanceSpinner) {
mapIntent = new Intent(this, Map.class);
mapIntent.setData(intent.getData());
mapIntent.putExtra("Distance", pos);
startActivity(mapIntent);
finish();
}
}
}

关于java - 如何在可搜索字典示例中 putExtra(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2918003/

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