1

私はこのコードを持っています

go_btn.setOnClickListener(new View.OnClickListener() {

          public void onClick(View v) {
              Intent myIntent = new Intent(LoginActivity.this, WebActivity.class);
              myIntent.putExtra("id", ed_txt.getText().toString());
              myIntent.putExtra("type", spinner.getText().toString());
              LoginActivity.this.startActivity(myIntent);

スピナーで選択したオプションを文字列に追加したい。

これは私にエラーを与える

myIntent.putExtra("type", spinner.getText().toString());

私が使うとき

myIntent.putExtra("type", spinner.getContext().toString());

パッケージ名を取得しています。

選択したアイテムを取得するにはどうすればよいですか

4

3 に答える 3

1

使用する

myIntent.putExtra("type", spinner.getSelectedItem().toString());

代わりに

myIntent.putExtra("type",spinner.getText().toString());

スピナーから選択したアイテムのテキストを取得するため

于 2012-12-09T08:45:20.067 に答える
0

このコードを確認してください。このコードが役立つことを願っています。

String getSelectItem = "null";

Spinner spinner = (Spinner) findViewById(R.id.[あなたのスピナーID]);

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        getSelectItem = spinner.getSelectedItem().toString();
    }
    public void onNothingSelected(AdapterView<?> parent) {
    }
});
于 2012-12-09T15:45:49.593 に答える
0

選択した項目を取得するには、リスナーをスピナーに追加する必要があります。それはこのようなものでなければなりません

String selection = null;
//Initialise Spinner (you may have done this already)

Spinner spinner = (Spinner) findViewById(R.id.[your spinner id]);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            selection = parent.getItemAtPosition(pos).toString();
        }
        public void onNothingSelected(AdapterView<?> parent) {
        }
    })

次に、「選択」文字列をインテントに入れることができます。幸運を :)

于 2012-12-09T13:53:39.887 に答える