【问题标题】:How to set the bundle value in Spinner view?如何在 Spinner 视图中设置捆绑包值?
【发布时间】:2021-09-04 05:59:01
【问题描述】:

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

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

//国家数据

 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
    }

【问题讨论】:

  • ArrayAdapter(this.getActivity(), R.layout.spinner_text, country) 这里的国家是什么。您在国家对象中声明和赋值的位置
  • @DharmenderManral ,我更新了问题,请看一眼。
  • @DharmenderManral 以下提供的代码不包括在微调器中设置捆绑值的任何方法。所以请仔细阅读问题。
  • 你能告诉我你如何以及在哪里将数据放在包中,并确保国籍对象不应该为空
  • 我已经更新了我的答案,你能再检查一下吗

标签: java android fragment bundle spinner


【解决方案1】:

用下面的代码试试它的工作原理。

  //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");

    }
  }


 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-10
    • 2016-07-15
    • 1970-01-01
    • 2016-12-13
    相关资源
    最近更新 更多