0

我希望在微调器菜单中选择的选项显示在 id 为“spinnertxt”的文本视图中。

我可以打开微调器菜单并选择该选项,但之后没有任何反应。

这是代码:

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 {

protected int mPos;
protected 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);

}

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

    //NOTHING TO LOOK HERE, MOVE ALONG

}

  }

有人能告诉我为什么吗?我是新手。

4

2 回答 2

0

实例化您的数组并使用

Addproject.this.mSelection = array[pos];

代替

Addproject.this.mSelection = parent.getItemAtPosition(pos).toString();
于 2012-12-23T17:02:08.070 回答
0

解决了!

一个听众不见了。

我刚刚添加

spinner.setOnItemSelectedListener(this);

onCreate()之后_setAdapater()

需要侦听器,以便应用程序侦听对特定项目的点击。

于 2012-12-23T19:43:42.820 回答