3

特定の値リストを持つ 3 つのスピナーを作成しました。各 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 に答える