来自微调器的选定选项未在TextView中显示

时间:2012-12-23 19:20:38

标签: android android-widget android-spinner

我希望我选择的微调器选项显示在同一活动的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  

}



}

1 个答案:

答案 0 :(得分:2)

尝试添加

spinner.setOnItemSelectedListener(this);
在此行之后的onCreate()

 spinner.setAdapter(adapter);

onCreate()中,你已经制作了旋转器,但没有给它一个听众。

这意味着

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

onItemSelected()

中不需要