tags:

views:

192

answers:

1

Hi, 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);
    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

+1  A: 

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();      
    }
  }
}
Deepank Gupta
Thank you for this idea but it doesn't work.At first the user choose some settings in the spinner, then he press the search button and search for something. If he press on a searchresult, the new intent should start.With you idea the new intent starts befor he can select a search result.
sirlunchalot