微调器默认值/更改位置

时间:2014-07-30 20:02:55

标签: java android

社区大家好,

我的项目实施遇到了问题,我希望有人可以给我一些清晰度。 我项目的简短描述: 我想从Activity_A的一个按钮onclick事件更改Activity_B中微调器的默认位置。在我的示例代码中,我尝试在一个if - to给出语句中使用Intent,Value Activity_A到Activity_B中的微调器,然后在相应的位置启动微调器。

这是我的代码:

Activity_A:

public class Start extends Activity {


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.start);            

        Button button = (Button) findViewById(R.id.button_1);
        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                Intent intent = new Intent(Start.this, Spinnerwert.class);          
                intent.putExtra("position", "3");
                startActivity(intent);


            }
        });    



        }

    }

Activity_B:

public class Spinnerwert extends Activity {


    private TextView beschreibung_1;
    private TextView beschreibung_2;

    private Spinner s1;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spinner_wert);

        // TextView issue of Spinner
        beschreibung_1 =(TextView)findViewById(R.id.tV_spinner_1);  

        // TextView output Intent worth 
        beschreibung_2 =(TextView)findViewById(R.id.tV_spinner_2);


        // Intent data receiving
        Intent i = getIntent();
        String selected = i.getStringExtra("position");     
        beschreibung_2.setText(selected);       


        s1 = (Spinner) findViewById(R.id.spinner_1);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,         R.array.auswahl, android.R.layout.simple_spinner_item); 
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s1.setAdapter(adapter);
        s1.setOnItemSelectedListener(new OnItemSelectedListener() {



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

        // Spinner position - Response to Intent worth handover --------

            s1.setSelection(position);


            if (position == 0) {                    
                s1.setSelection(0);

            } else if (position == 1) {   
                s1.setSelection(1);

            } else if (position == 2) {    
                s1.setSelection(2); 

            }

        beschreibung_1.setText((CharSequence) parent.getItemAtPosition(position));  

        }

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

        }

    });  


    }

}

3 个答案:

答案 0 :(得分:1)

好像你的意图是传递一个String extra,而只是改变第二个活动中的文本视图。如果我理解您的问题是正确的,那么您希望根据第一个活动传递的意图将微调器置于某个位置。

使用类似于IF语句中的调用,只需将选定的String解析为int并在其上调用s1.setSelection。

看起来应该是这样的:

    s1 = (Spinner) findViewById(R.id.spinner_1);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,         R.array.auswahl, android.R.layout.simple_spinner_item); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s1.setAdapter(adapter);
    s1.setSelection(Integer.parseInt(selection));

我希望这有帮助!

答案 1 :(得分:0)

您似乎永远不会将微调器设置为所选索引。你在onItemSelected中的if语句并没有做任何事情,因为&#34; position&#34;是一个从微调器传入的值,而不是之前的意图。

尝试把这个

s1.setSelection(Integer.parseInt(selected));

设置适配器后。

答案 2 :(得分:0)

具有等值

的Spinner变体
public class Spinnerwert_2 extends Activity {

    private TextView beschreibung_3;
    private TextView beschreibung_4;

    private Spinner s2;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spinner_wert_2);


        beschreibung_3 =(TextView)findViewById(R.id.tV_spinner_3);
        beschreibung_4 =(TextView)findViewById(R.id.tV_spinner_4);


        Intent intent_spinner_2 = getIntent();       
        String spinner_2_auswahl = intent_spinner_2.getStringExtra("position_spinner_2");
        beschreibung_4.setText(spinner_2_auswahl);       


        // #2 Spinner + Funktion(Auswahl aus Resource values/array.xml)

        s2 = (Spinner) findViewById(R.id.spinner_2);
        ArrayAdapter<CharSequence> adapter_2 = ArrayAdapter.createFromResource(this, R.array.auswahl, android.R.layout.simple_spinner_item); 
        adapter_2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s2.setAdapter(adapter_2);

        // #2 SPINNER-ITEM POSITIONSEINSTELLUNG / ZUWEISUNG  
        s2.setSelection(adapter_2.getPosition(spinner_2_auswahl));

        s2.setOnItemSelectedListener(new OnItemSelectedListener() {


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


            if (s2.getSelectedItem().toString().equals("Mercury")){

                Toast.makeText(getApplicationContext(), "Auswahl Mercury", Toast.LENGTH_SHORT).show();                

            } else if (s2.getSelectedItem().toString().equals("Venus")){                              

              Toast.makeText(getApplicationContext(), "Auswahl Venus", Toast.LENGTH_SHORT).show();

            } else if (s2.getSelectedItem().toString().equals("Earth")){

                Toast.makeText(getApplicationContext(), "Auswahl Earth", Toast.LENGTH_SHORT).show();


            } 

        beschreibung_3.setText((CharSequence) parent.getItemAtPosition(position));

        }

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

        }

    });  



    }

}