Almacenamiento de selecciones giratorias

I've created three Spinners with certain list of values. I store each Spinner's selection value in a variable. The problem is when I go to the next page I lose my variables. What should I do?

public class TabOneActivity extends Activity implements OnItemSelectedListener{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabone);

        // Spinner element
        Spinner spinner = (Spinner) findViewById(R.id.fudtaste);

        // Spinner click listener
        spinner.setOnItemSelectedListener(this);

        // Spinner Drop down elements
        List<String> categories = new ArrayList<String>();
        categories.add("    Chineese");
        categories.add("    Fast Food");
        categories.add("    Traditional");
        categories.add("    Mixed");
        categories.add("    Favourites");
        categories.add("    Combo Items");

        //Toast.makeText(getApplicationContext(),newPos1,Toast.LENGTH_LONG).show();
     //  System.out.println(str + "sarath" );

        Spinner spinner2 = (Spinner) findViewById(R.id.foodmoney);

        // Spinner click listener
        spinner2.setOnItemSelectedListener(this);

        // Spinner Drop down elements
        List<String> categories2 = new ArrayList<String>();
        categories2.add("    Fast Food");
        categories2.add("    Pub Food");
        categories2.add("    High End Food");
//        categories2.add("    ");
//        categories2.add("    Music With Food");
//        categories2.add("    Enjoy The Food");
        int newPos2 = spinner2.getSelectedItemPosition();

        //Toast.makeText(getApplicationContext(),newPos2,Toast.LENGTH_LONG).show();
        System.out.println(newPos2 + "sarath" );

        Spinner spinner3 = (Spinner) findViewById(R.id.transport);

        // Spinner click listener
        spinner3.setOnItemSelectedListener(this);

        // Spinner Drop down elements
        List<String> categories3 = new ArrayList<String>();
        categories3.add("    I'll Walk");
        categories3.add("    A Cab Please");
        categories3.add("    Limo!!");
        int newPos3 = spinner3.getSelectedItemPosition();

        //Toast.makeText(getApplicationContext(),newPos3,Toast.LENGTH_LONG).show();
        System.out.println(newPos3 + "sarath" );

        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
        ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories2);
        ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories3);

        // Drop down layout style - list view with radio button
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        spinner.setAdapter(dataAdapter);
        spinner2.setAdapter(dataAdapter2);
        spinner3.setAdapter(dataAdapter3);

        Button bt1 = (Button)findViewById(R.id.search1);
        bt1.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
                    Intent j = new Intent(getApplicationContext(), PlanMyDayListActivity.class);
                    j.putExtra("tab", 1);

                    startActivity(j);
            }
        });
    }

//  public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//      // On selecting a spinner item
//      String item = parent.getItemAtPosition(position).toString();
//      
////         Context mContext = null;
////        Toast.makeText(mContext,item,Toast.LENGTH_SHORT).show();
//      Toast.makeText(getApplicationContext(),item,Toast.LENGTH_LONG).show();
//
//  }
    public void onNothingSelected(AdapterView<?> arg0) {
    }

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

preguntado el 25 de agosto de 12 a las 12:08

3 Respuestas

prueba nuevo

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    TextView tv = (TextView)arg1;
    String str=tv.getText().toString();
}

respondido 03 nov., 12:04

yes this is perfect now i can store values into variables thankyou verymuch. - Desarrollador híbrido

but there ids another problem i cannot store separate spinner values in separate string variable in your statements it store both spinner selection value in a single string at the same time - Desarrollador híbrido

saayippinu manasilaavaan vendiyaanello code... codubhaasha!!! kodubhaasha adipoli - Niko

this is the complete solution of your query. If you think any doubt in this, then please let me know.

import android.widget.Toast;

public class TabOneActivity extends Activity implements OnItemSelectedListener{

    List<String> categories,categories2,categories3;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabone);

        // Spinner element
        Spinner spinner = (Spinner) findViewById(R.id.fudtaste);

        // Spinner click listener
        spinner.setOnItemSelectedListener(this);

        // Spinner Drop down elements
         categories = new ArrayList<String>();
        categories.add("    Chineese");
        categories.add("    Fast Food");
        categories.add("    Traditional");
        categories.add("    Mixed");
        categories.add("    Favourites");
        categories.add("    Combo Items");

        //Toast.makeText(getApplicationContext(),newPos1,Toast.LENGTH_LONG).show();
        //  System.out.println(str + "sarath" );

        Spinner spinner2 = (Spinner) findViewById(R.id.foodmoney);

        // Spinner click listener
        spinner2.setOnItemSelectedListener(this);

        // Spinner Drop down elements
         categories2 = new ArrayList<String>();
        categories2.add("    Fast Food");
        categories2.add("    Pub Food");
        categories2.add("    High End Food");
        //        categories2.add("    ");
        //        categories2.add("    Music With Food");
        //        categories2.add("    Enjoy The Food");
        int newPos2 = spinner2.getSelectedItemPosition();

        //Toast.makeText(getApplicationContext(),newPos2,Toast.LENGTH_LONG).show();
        System.out.println(newPos2 + "sarath" );

        Spinner spinner3 = (Spinner) findViewById(R.id.transport);

        // Spinner click listener
        spinner3.setOnItemSelectedListener(this);

        // Spinner Drop down elements
         categories3 = new ArrayList<String>();
        categories3.add("    I'll Walk");
        categories3.add("    A Cab Please");
        categories3.add("    Limo!!");
        int newPos3 = spinner3.getSelectedItemPosition();

        //Toast.makeText(getApplicationContext(),newPos3,Toast.LENGTH_LONG).show();
        System.out.println(newPos3 + "sarath" );

        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
        ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories2);
        ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories3);

        // Drop down layout style - list view with radio button
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        spinner.setAdapter(dataAdapter);
        spinner2.setAdapter(dataAdapter2);
        spinner3.setAdapter(dataAdapter3);

        Button bt1 = (Button)findViewById(R.id.search1);
        bt1.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
                Intent j = new Intent(getApplicationContext(), PlanMyDayListActivity.class);
                j.putExtra("tab", 1);

                startActivity(j);
            }
        });
    }

    //  public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    //      // On selecting a spinner item
    //      String item = parent.getItemAtPosition(position).toString();
    //      
    ////         Context mContext = null;
    ////        Toast.makeText(mContext,item,Toast.LENGTH_SHORT).show();
    //      Toast.makeText(getApplicationContext(),item,Toast.LENGTH_LONG).show();
    //
    //  }
    public void onNothingSelected(AdapterView<?> arg0) {
    }

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

        switch(arg1.getId()){
        case R.id.fudtaste:
        {
            Toast.makeText(getApplicationContext(),"spinner clicked value: "+categories.get(arg2),Toast.LENGTH_LONG).show();
            break;
        }
        case R.id.foodmoney:
        {
            Toast.makeText(getApplicationContext(),"spinner2 clicked value: "+categories2.get(arg2),Toast.LENGTH_LONG).show();
            break;
        }
        case R.id.transport:
        {
            Toast.makeText(getApplicationContext(),"spinner3 clicked value: "+categories3.get(arg2),Toast.LENGTH_LONG).show();
            break;
        }
        }

    }

}

respondido 03 nov., 12:04

Sorry dude for this late answer!

Use shared preference

Respondido 10 Jul 13, 10:07

poda potta njan chothichathu engana spinneril select chaitha value get cheyyunnathu ennaanu - Desarrollador híbrido

chodikkaan ariyaatha thendi! - Niko

athu vaaicha englishkarkku kkaryam manasillai ennittum balavishnu saaippinu pudikittilla polum.............. - Desarrollador híbrido

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.