设置微调器选定的项目文本

时间:2015-11-12 10:10:09

标签: android android-spinner settext onitemselectedlistener

我有一个微调器(只是代码的相关部分)......

protected void onCreate(Bundle savedInstanceState) {

   Spinner to_spinner = (Spinner) findViewById(R.id.to_spinner);
   List<Unit> list = myDbHelper.getAllUnits();
   SpinnerUnitAdapter tUnitAdapter tUnitAdapter = new SpinnerUnitAdapter(this, android.R.layout.simple_spinner_item, list);

   to_spinner.setAdapter(tUnitAdapter);
   to_spinner.setOnItemSelectedListener(onItemSelectedListenerTo);
}

使用onItemSelectedListener

AdapterView.OnItemSelectedListener onItemSelectedListenerTo = new     AdapterView.OnItemSelectedListener() {

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

        // do stuff
    }

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

当选择一个项目时(即它说:&#34;做东西&#34;),我想设置/更改所选微调项目的文本。 (注意,与设置微调器位置不一样(使用setSelection())

我想用

做这件事
tUnitAdapter.getView(position, ?, ?).setText("new text");

我是否在正确的轨道上?在getView中放置第二个(&#34; convertView&#34;)和第三个(&#34; parent&#34;)参数的内容。我的微调器适配器看起来像:

public class SpinnerUnitAdapter extends ArrayAdapter<Unit> {
     ...
     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
         TextView label = new TextView(mContext);
         label.setTextColor(Color.BLACK);                        
       label.setTextSize(mContext.getResources().getDimension(R.dimen.list_row_font_size));
         label.setGravity(Gravity.CENTER);
         label.setText(getItem(position).getName());

         return label;
    }
}

3 个答案:

答案 0 :(得分:2)

Am I on the right track?

No. You should do the following steps: (in the onItemSelected method)

  • Update your model (the array of items you passed to the adapter) so that the item at position index takes the new name.

  • Issue notifyDataSetChanged on the adapter object. Alternatively, you can do this manually by ((TextView) view).setText(new_name);

Note: In onItemSelected method, adapterView points to your spinner view and view points to the row view just selected.


UPDATE #1

public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
    ((TextView) view).setText("new name");
}

UPDATE #2

For this, you should use Java interface to implement a callback which is called once the dialog is closed.

public void onItemSelected(AdapterView<?> adapterView, final View view, int position, long id) {
    Dialog dialog = new MyDialog(context, new MyDialog.OnItemSelectListener(){
        @Override
        public void onItemSelected(String newName){
            ((TextView) view).setText(newName);
        }
    });
    dialog.show();
}

And declare interface OnItemSelectListener in your MyDialog class.

答案 1 :(得分:0)

您可以在单击每个项目时更改微调器的文本,以便实现onClickListener

public class SpinnerUnitAdapter extends ArrayAdapter<Unit> {
         ...
         @Override
         public View getView(int position, View convertView, ViewGroup parent) {
             TextView label = new TextView(mContext);
             ...
             label.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                  // change label text here
                  // label.setText(...);
                }
             });
             return label;
        }
    }

希望这个帮助

答案 2 :(得分:0)

I think I found the solution. I can just do:

public void onItemSelected(AdapterView<?> adapterView, View view,
                               int position, long id) {
    TextView tv = (TextView) adapterView.getSelectedView();
    tv.setText("new text");
}