无法显示通过微调器选择的选项

时间:2012-12-23 16:55:14

标签: java android

我希望在微调器菜单中选择的选项显示在id为'spinnertxt'的textview中。

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

以下是代码:

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

}

  }

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

2 个答案:

答案 0 :(得分:0)

实例化您的数组并使用

Addproject.this.mSelection = array[pos];

而不是

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

答案 1 :(得分:0)

解决!

听众失踪了。

我刚刚添加了

spinner.setOnItemSelectedListener(this);
onCreate()

之后

setAdapater()

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