2010-11-11 10 views
0

Je suis actuellement sur mon premier Spinner. Mais je suis resté coincé pendant l'onItemSelectedListener, puisque je ne peux pas l'implémenter. J'ai d'abord essayé de suivre la méthode du livre CommonWares mais cela fonctionnerait - mais ma méthode ne fonctionne pas non plus. Au début, j'ai essayé de laisser mon activité implémenter le AdapterView directement - mais la seule conséquence était qu'eclipse m'a dit que l'interface AdapterView n'est pas disponible et m'a demandé de la créer ... mais j'ai eu la même erreur maintenant encore.AdapterView not found [Spinner]

public class Lunchplace extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.mensa); 
    Context c = getApplicationContext(); 

    Spinner dateSelection = (Spinner)findViewById(R.id.date); 

    //ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.) 
    // get all the little tidbits of extra informations 
    Bundle extras = getIntent().getExtras(); 

    String location = extras.getString("Mensa"); 
    // this function will download the Lunchfile - if necessary 
    Data lunchData = new XMLData(c); 

    // set the header text 
    TextView mensaname = (TextView)findViewById(R.id.header); 
    mensaname.setText(location); 

    // get the spin view out of the xml 
    Spinner spin = (Spinner)findViewById(R.id.date); 

    // attach it to an adapter 
    ArrayAdapter adapter = ArrayAdapter.createFromResource(
      this, R.array.days, android.R.layout.simple_spinner_item); 
    // I should be able to put a custom layout of the spinner in there.. I bet 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spin.setAdapter(adapter); 


    spin.setOnClickListener(
      new AdapterView.OnItemSelectedListener() {   

      @Override 
      public void onItemSelected(AdapterView<?> parent, 
      View view, int position, long id) { 
      } 

     @Override 
     public void onNothingSelected(AdapterView<?> arg0) { 
      // TODO Auto-generated method stub 

     } 
      } 
    ); 

    // set the current day of the week as the default selection 
    spin.setSelection(Tools.getDayOfWeek()); 

    // get the tablelayout 
    TableLayout tl = (TableLayout)findViewById(R.id.MenuTable); 
    lunchData.getMenuforDay(c,tl,location); 

    TextView counterTV = new TextView(c, null, R.style.MenuField); 

    } 
} 

Quelqu'un at-il une idée sur la façon dont je peux résoudre ce problème?

Répondre

0

L'implémentation de OnItemSelectedListener n'a rien de spécial. Essayez comme ceci:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
      if ("YourDayToHandle".equals(spinner.getSelectedItem())) { 
       // do smth useful here 
      } 
     } 

    public void onNothingSelected(AdapterView<?> parent) {} 
});