将TextView文本设置为Spinner所选项

时间:2014-07-14 21:20:31

标签: java android eclipse spinner

如何将TextView的文本设置为Spinner的所选文本?以下是我的代码的相关部分。现在我从Spinner中选择一个项目,但TextView文本没有改变。

public void onCreate(etc, etc...
{
  Spinner spinner = (Spinner)findViewById(R.id.mySpinner);
  List<String> myList = myClass.myList();

  ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, myList);        
  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  spinner.setAdapter(myAdapter);

  SpinnerListener(spinner);    


// Here I try to set the TextView text to the Spinner's selected text, but it doesn't work (the text doesn't change):

  TextView tv= (TextView)findViewById(R.id.myTextView);     
  tv.setText(spinner.getSelectedItem().toString()); 


}

Spinner听众:

private void SpinnerListener(Spinner spinner)
    {
        spinner.setOnItemSelectedListener(new mySelectedListener());
    }


public class mySelectedListener implements OnItemSelectedListener {

    @Override
    public void onItemSelected(AdapterView parent, View view, int pos, long id) {

        Toast.makeText(parent.getContext(), parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();           
    }

    @Override
    public void onNothingSelected(AdapterView parent) {             
    }

}

4 个答案:

答案 0 :(得分:0)

您应该将tv.setText(spinner.getSelectedItem().toString());onCreate移到public void onItemSelected(...),得到以下内容: tv.setText(parent.getItemAtPosition(pos).toString())。 看它是否有效!

问题是,如果你把代码放在onCreate上,它只会在第一次创建活动/片段时被调用,因此如果用户在微调器上选择一个项目,它就永远不会改变。

答案 1 :(得分:0)

使用此示例

public class MainActivity extends Activity {


String[] text1 = { "SUNDAY", "MONDAY", "TUESDAY",
        "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" };
int[] val1 = { 0, 1, 2, 3, 4, 5, 6};


Spinner  spinner1;
TextView  textView1;

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

    textView1 = (TextView)findViewById(R.id.text1);
    spinner1 = (Spinner)findViewById(R.id.spinner1);
    ArrayAdapter<String> adapter1 =
            new ArrayAdapter<String>(MainActivity.this,
                    android.R.layout.simple_spinner_item, text1);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter1);
    spinner1.setOnItemSelectedListener(onItemSelectedListener1);
}

OnItemSelectedListener onItemSelectedListener1 =
        new OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                String s1 = String.valueOf(val1[position]);
                textView1.setText(s1);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}

        };


      }

XML CODE

 <Spinner
    android:id="@+id/spinner1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<TextView
    android:id="@+id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />


  </LinearLayout>

答案 2 :(得分:0)

TextView spinget= (TextView) findViewById(R.id.spinget);

  Spinner  destspin1= (Spinner) findViewById(R.id.destspin);

destspin1.setOnItemSelectedListener(this);

 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

       String s=destspin1.getSelectedItem().toString();
        spinget.setText(s);

    }

它会起作用

答案 3 :(得分:0)

将TextView文本设置为Spinner所选项

Spinner xyzSPN =(Spinner)view.findViewById(R.id.spn_xyz_spn); TextView xyzTV =(Spinner)view.findViewById(R.id.spn_xyz_tv);

//在微调器上设置列表器并获取所选文本

xyzSPN.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){             @覆盖             public void onItemSelected(AdapterView parentView,View selectedItemView,int position,long id){

            xyzTV.setText(xyzSPN.getSelectedItem().toString());
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }
    });
}