微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Spinner 视图中设置 bundle 值?

如何解决如何在 Spinner 视图中设置 bundle 值?

编辑配置文件片段中有一个微调视图

我想在微调器中设置国籍的用户数据(从包中检索的值)。
我的尝试是:

//国家数据

 String[] country = {"Country","Afghanistan","Albania","Algeria.......... };

//旋转器

    private void setSpinner(View view) {
        // this is the spinner of country name
        Spinner spin = view.findViewById(R.id.et_address);
        ArrayAdapter aa = new ArrayAdapter(this.getActivity(),R.layout.spinner_text,country);
        aa.setDropDownViewResource(simple_spinner_dropdown_item);
        //Setting the ArrayAdapter data on the Spinner
        spin.setAdapter(aa);
spin.setonItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent,View view,int position,long id) {

                if (position != 0) {
                    spinnerValue = country[position];

                    System.out.println("country position is" + spinnerValue);
                    ((TextView) parent.getChildAt(0)).setTextColor(Color.BLACK);

                }
            }

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

            }
        });
    }

//使用bundle从ProfileFragments中获取数据

 public void getBundleData() {
        bundle = this.getArguments();
        if (bundle != null) {
            clientFullname1 = bundle.getString("client_fullname");
            dob1 = bundle.getString("dob");
            email1 = bundle.getString("email");
            phone1 = bundle.getString("phone");
            nationality1 = bundle.getString("nationality");

        }
    }

//设置从bundle中检索到的数据到textView

 public void setValue() {
        etFullName.setText(clientFullname1);
        etDateOfBirth.setText(dob1);
        etEmailAddress.setText(email1);
        etPhoneDetails.setText(phone1);

        etAddress.setText(nationality1);  //Note: the author has set this nationality value in 
                                           textView instead of spinner but he is trying to set the bundle value in spinner
    }

解决方法

尝试使用以下代码工作。

  //activity xml
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myToolbar">
<Spinner
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:id="@+id/spinner01"/></RelativeLayout>

 //Activity code
  public class MainActivity extends AppCompatActivity {
private Spinner spinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    getBundleData();
    spinner = findViewById(R.id.spinner01);
    ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,new String[]{nationality1});
    spinner.setAdapter(aa);


    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent,View view,int position,long id) {

            if (position != 0) {
                Toast.makeText(MainActivity.this,"click on spinner item",Toast.LENGTH_LONG).show();

            }
        }

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

        }
    });

  }


public void getBundleData() {
    bundle = this.getArguments();
    if (bundle != null) {
        clientFullname1 = bundle.getString("client_fullname");
        dob1 = bundle.getString("dob");
        email1 = bundle.getString("email");
        phone1 = bundle.getString("phone");
        nationality1 = bundle.getString("nationality");

    }
  }


 }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。