How to putExtra() in Searchable Dictionary Example
based on the Searchable Dictionary sample I tried to put extra data to a different 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);
开发者_StackOverflow中文版 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();
}
}
In my Map Class Distance is always zero because distance.getSelectedItemPostion() gets the initialized value. How can I putExtra data with a click on a search suggestion? Thanks
You have to register a callback on value change of distance spinner and call the intent for the starting of the Map class activity from there.
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();
}
}
}
精彩评论