【发布时间】: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