3

我创建了三个具有特定值列表的 Spinners。我将每个 Spinner 的选择值存储在一个变量中。问题是当我转到下一页时,我丢失了我的变量。我该怎么办?

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) {
    }
}
4

3 回答 3

2

尝试新的

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    TextView tv = (TextView)arg1;
    String str=tv.getText().toString();
}
于 2012-08-29T12:38:15.480 回答
0

这是您查询的完整解决方案。如果您对此有任何疑问,请告诉我。

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;
        }
        }

    }

}
于 2012-11-03T04:19:21.163 回答
0

对不起,这个迟到的答案!

使用共享偏好

于 2013-07-10T09:54:13.413 回答