在另一个类TextView中显示Spinner所选项

时间:2016-02-28 10:40:30

标签: java android spinner

所以我有一个Spinner,我不想用它从中选择一个项目,然后在另一个活动中显示他的选择..但Button甚至不做任何事情......我猜错误是在intent.putExtra int position == 0 ..有人可以帮帮我吗?

cilj = (Spinner) findViewById(R.id.spinnerPrehranaCilj);
            prikaziRezultat = (Button) findViewById(R.id.buttonPrehranaPrikaziRezultate);


            ArrayAdapter<CharSequence> ciljPrehranaSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_cilj_prehrana, android.R.layout.simple_spinner_item);
            ciljPrehranaSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            cilj.setAdapter(ciljPrehranaSpinnerAdapter);

            prikaziRezultat.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    cilj.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                            if (position == 0){
                                Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
                                intent.putExtra("ciljJePovecanjeTezine", cilj.getItemIdAtPosition(0));
                                startActivity(intent);
                            } else if (position == 1){
                                Toast.makeText(PrehranaInputMain.this, "drugo bi trebalo bit odabrano", Toast.LENGTH_SHORT).show();
                            }

                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {

                        }
                    });

                }
            });

这是第二项活动

 String prehrana = intent.getStringExtra("ciljJePovecanjeTezine");

        ciljPrehranaRezultat = (TextView) findViewById(R.id.textViewPrehranaCiljRezultat);
        ciljPrehranaRezultat.setText(prehrana);

谢谢!

以下是第一项活动的修改代码:

 cilj = (Spinner) findViewById(R.id.spinnerPrehranaCilj);
        prikaziRezultat = (Button) findViewById(R.id.buttonPrehranaPrikaziRezultate);


        ArrayAdapter<CharSequence> ciljPrehranaSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_cilj_prehrana, android.R.layout.simple_spinner_item);
        ciljPrehranaSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        cilj.setAdapter(ciljPrehranaSpinnerAdapter);


                cilj.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        if (position == 0){
                            Toast.makeText(PrehranaInputMain.this, R.string.cilj_prehrana_nista_odabrano, Toast.LENGTH_SHORT).show();
                        } else if (position == 1){
                            Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
                            intent.putExtra("ciljJePovecanjeTezine", cilj.getSelectedItem().toString());
                            startActivity(intent);
                        } else if (position == 2){
                            Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
                            intent.putExtra("ciljJeMrsavljenje", cilj.getSelectedItem().toString());
                            startActivity(intent);
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
        prikaziRezultat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

和第二项活动

      String prehrana = intent.getStringExtra("ciljJePovecanjeTezine");
            String prehrana2 = intent.getStringExtra("ciljJeMrsavljenje");

            ciljPrehranaRezultat = (TextView) findViewById(R.id.textViewPrehranaCiljRezultat);
            ciljPrehranaRezultat.setText(prehrana);

// SEEMS LIKE SOME IF STATEMENT NEEDED HERE?
            //ciljPrehranaRezultat.setText(prehrana2);

2 个答案:

答案 0 :(得分:1)

直到用户点击按钮才会设置下拉列表的监听器,这意味着在您至少单击按钮一次之前,从下拉列表中选择一个项目不会做任何事情。

您是否尝试在单击按钮后从微调器中选择一个项目?

如果要在用户单击按钮时转到其他活动,请仅在onClick()方法中保留startActivity(intent)并将onItemSelected()方法向上移动一级,将intent作为全局变量移动

cilj = (Spinner) findViewById(R.id.spinnerPrehranaCilj);
prikaziRezultat = (Button)      findViewById(R.id.buttonPrehranaPrikaziRezultate);
Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);

ArrayAdapter<CharSequence> ciljPrehranaSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_cilj_prehrana, android.R.layout.simple_spinner_item);
ciljPrehranaSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cilj.setAdapter(ciljPrehranaSpinnerAdapter);

                cilj.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                        if (position == 0){
                            intent.putExtra("ciljJePovecanjeTezine", cilj.getItemIdAtPosition(0));
                        } else if (position == 1){
                            Toast.makeText(PrehranaInputMain.this, "drugo bi trebalo bit odabrano", Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });

        prikaziRezultat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                            startActivity(intent);
            }
        });

另外,即使在这种情况下,在我看来,当用户选择位置“0”的项目时,您只是跳到活动。我们可以假设这不是一个空格,你的微调器只有2个条目吗?

答案 1 :(得分:0)

更改以下行:

if (position == 0){
      Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
      intent.putExtra("ciljJePovecanjeTezine", (CharSequence)cilj.getItemIdAtPosition(0));
      startActivity(intent);
} else if (position == 1){
      Toast.makeText(PrehranaInputMain.this, "drugo bi trebalo bit odabrano", Toast.LENGTH_SHORT).show();
}

要:

Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
intent.putExtra("ciljJePovecanjeTezine", cilj.getItemAtPosition(position));
startActivity(intent);