0

我希望我选择的微调器选项显示在同一活动的 TextView 中。单击微调器时,多个选项确实会下降,但在选择时没有任何反应;TextView 保持不变。

代码:

package com.kk.project;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class Addproject extends Activity implements OnItemSelectedListener {

int mPos;
String mSelection;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_addproject);

    Spinner spinner = (Spinner) findViewById(R.id.difficultyspinner); ArrayAdapter<CharSequence> 
    adapter = ArrayAdapter.createFromResource(this,R.array.difficultyarray, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_addproject, menu);
    return true;
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    // TODO Auto-generated method stub

    Addproject.this.mPos = pos;
    Addproject.this.mSelection = parent.getItemAtPosition(pos).toString();

    TextView spinnerresult = (TextView)findViewById(R.id.spinnertxt);
    spinnerresult.setText(Addproject.this.mSelection);

    Spinner spinner = (Spinner)findViewById(R.id.spinnertxt);
    spinner.setOnItemSelectedListener(this);

}

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

    //NOTHING  

}



}
4

1 回答 1

2

尝试添加

spinner.setOnItemSelectedListener(this);

在你的onCreate()这一行之后:

 spinner.setAdapter(adapter);

onCreate()中,您已经制作了您的微调器,但没有给它一个监听器。

这意味着

Spinner spinner = (Spinner)findViewById(R.id.spinnertxt);
spinner.setOnItemSelectedListener(this);

不应该在onItemSelected()

于 2012-12-23T19:24:37.617 回答