我希望我选择的微调器选项显示在同一活动的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
}
}
答案 0 :(得分:2)
尝试添加
spinner.setOnItemSelectedListener(this);
在此行之后的onCreate()
中:
spinner.setAdapter(adapter);
在onCreate()
中,你已经制作了旋转器,但没有给它一个听众。
这意味着
Spinner spinner = (Spinner)findViewById(R.id.spinnertxt);
spinner.setOnItemSelectedListener(this);
onItemSelected()