Domanda

Sono nuovo su Android, sto cercando di memorizzare il valore dello spinner nel database, ma ricevo un errore durante l'archiviazione nel database.qualcuno può aiutarmi per favore. ecco il mio codice,

mGender = (Spinner)findViewById(R.id.spinner1);
String gender = mGender.toString();
values.put("gender", gender);

ho cambiato il codice, quindi posso leggere il valore dello spinner, ma quando controllo il mio database non mostra le informazioni esatte fornite nello spinner, mostra qualcosa di simile

android.widget.Spinner@41372738
android.widget.Spinner@41382ae0

per gli stessi valori.Qualcuno può aiutarmi.

grazie in anticipo

È stato utile?

Soluzione

finalmente ho trovato la risposta a questa domanda passando attraverso i diversi tutorial ed esempi.la soluzione per questo è:

mGender = (Spinner)findViewById(R.id.spinner1);

        // Spinner method to read the on selected value
        ArrayAdapter<State> spinnerArrayAdapter = new ArrayAdapter<State>(this,
                  android.R.layout.simple_spinner_item, new State[] {   
                        new State("Male"), 
                        new State("Female")});
        mGender.setAdapter(spinnerArrayAdapter);
        mGender.setOnItemSelectedListener(this);

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
    {
        // Get the currently selected State object from the spinner
        State st = (State)mGender.getSelectedItem();

        // Show it via a toast
        toastState( "onItemSelected", st );
    } 

public void toastState(String name, State st) 
{
    if ( st != null )
    {
        Gen = st.name;
    //Toast.makeText(getBaseContext(), Gen, Toast.LENGTH_SHORT).show();

    }

}

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

}

devi creare una casella di selezione e assegnare i valori nel metodo onCreate.e un altro stato di classe per leggere i valori dello spinner.

public class State 
{
    public String name = "";


    public State(String _name)
    {

        name = _name;

    }
    public String toString()
    {
        return name;
    }


}

Grazie a tutti ....

Altri suggerimenti

category =  (Spinner)findViewById(R.id.category_group);  

category_spinner= new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,
        getResources().getStringArray(R.array.category_value));
category.setAdapter(category_spinner);

category.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {

    sppiner_Text= category_spinner.getItem(arg2).toString();

    }

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

    }
});

 //onSaveButton Click you just insert the value in DB    
  insert(sppiner_Text);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top