ArrayAdapter 要求资源 ID 为 TextView,用于 Databinding 和 ConstraintLayout
ArrayAdapter requires the resource ID to be a TextView for Databinding and ConstraintLayout
我试图在文本输入布局中设置微调器,但我不明白为什么代码因未获取 TextView ID 而崩溃。我尝试了第 3 方库来设置像 TextInputLayout 这样的微调器,但我正在尝试通过自定义方式进行设置。
这里是xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.TextInputLayout
android:id="@+id/til_relationship"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.TextInputEditText
android:id="@+id/et_temp_relationship"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:hint="Gender"
android:longClickable="false" />
</android.support.design.widget.TextInputLayout>
<Spinner
android:id="@+id/relationWithBaby"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:nextFocusDown="@+id/gender"
android:paddingBottom="14dp"
android:paddingLeft="9dp"
android:paddingRight="9dp"
android:paddingTop="25dp"
app:layout_constraintBottom_toBottomOf="@+id/til_relationship"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/til_relationship" />
</android.support.constraint.ConstraintLayout>
</layout>
这是我的 activity 绑定代码(单向)
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private ActivityMainBinding mMainBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
initUI();
}
private void initUI() {
mMainBinding.relationWithBaby.setAdapter(new MyArrayAdapter(this, R.layout.custom_layout, R.id.custom_text, addEmptyElementToArray(getResources().getStringArray(R.array.user_gender))));
mMainBinding.relationWithBaby.setOnItemSelectedListener(this);
mMainBinding.relationWithBaby.setSelection(0);
}
private String[] addEmptyElementToArray(String[] array) {
List<String> list = new ArrayList<>(Arrays.asList(array));
list.add(0, "");
return (list.toArray(new String[list.size()]));
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
private class MyArrayAdapter extends ArrayAdapter<String> {
private int textViewResourceId;
private int customText;
public MyArrayAdapter(Context context, int textViewResourceId, int custom_text, String[] objects) {
super(context, textViewResourceId, objects);
this.textViewResourceId = textViewResourceId;
this.customText= custom_text;
}
@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view;
TextView textSpinnerName;
if (convertView == null) {
view = getLayoutInflater().inflate(textViewResourceId, parent, false);
} else {
view = convertView;
}
textSpinnerName = (TextView) view.findViewById(R.id.custom_text);
String item;
item = getItem(position);
textSpinnerName.setText(item);
return view;
}
}
}
这是我正在膨胀的自定义视图
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/custom_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
tools:text="Apple" />
</android.support.constraint.ConstraintLayout>
我知道我应该在适配器设置中提供 textview 的另一个参数,但不明白为什么它找不到该视图的 ID
对于那些想在 android 中的文本输入布局中使用微调器的人来说,这里是代码。
- 保持 XML 主文件不变 XML
- 从自定义微调器中删除约束布局XML
- 将此代码添加到您的 activity/fragment
适配器设置代码
mUserBinding.spinnerProfileGender.setAdapter(new MyArrayAdapter(getActivity(), R.layout.custom_spinner, addEmptyElementToArray(getResources().getStringArray(R.array.user_gender))));
mUserBinding.spinnerProfileGender.setOnItemSelectedListener(this);
mUserBinding.spinnerProfileGender.setSelection(0);
适配器代码
userCountryAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_dropdown_item, userCountry);
mUserBinding.spinnerProfileState.setAdapter(userCountryAdapter);
And here is final code for activity/fragment
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
for (int j = 0; j < parent.getChildCount(); j++) {
parent.getChildAt(0);
}
if (parent == mUserBinding.spinnerProfileGender) {
if (position != 0) {
mUserBinding.inputTextEditGender.setText(" ");
//TODO NOTE
//For sending selection to upload data used spinner data
Toast.makeText(getActivity(), " " + mUserBinding.spinnerProfileGender.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
}
} else if (parent == mUserBinding.spinnerProfileState) {
if (position != 0) {
mUserBinding.inputtextProfileState.setText(userCountry.get(position));
}
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
private class MyArrayAdapter extends ArrayAdapter<String> {
private int textViewResourceId;
public MyArrayAdapter(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
this.textViewResourceId = textViewResourceId;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view;
TextView text;
if (convertView == null) {
view = getLayoutInflater().inflate(textViewResourceId, parent, false);
} else {
view = convertView;
}
text = (TextView) view;
String item;
item = getItem(position);
text.setText((CharSequence) item);
if (position == 0) {
ViewGroup.LayoutParams l = text.getLayoutParams();
l.height = 1;
text.setLayoutParams(l);
} else {
ViewGroup.LayoutParams l = text.getLayoutParams();
//you can change height value to yours
l.height = 150;
text.setLayoutParams(l);
}
return view;
}
}
@NonNull
private String[] addEmptyElementToArray(String[] array) {
List<String> list = new ArrayList<>(Arrays.asList(array));
list.add(0, "");
return (list.toArray(new String[list.size()]));
}
在第三方微调库提供的 android 文本输入布局中设置微调只是一个小技巧。
并且在 Onitemselection 中,您可以从微调器位置而不是编辑文本中获取数据
我试图在文本输入布局中设置微调器,但我不明白为什么代码因未获取 TextView ID 而崩溃。我尝试了第 3 方库来设置像 TextInputLayout 这样的微调器,但我正在尝试通过自定义方式进行设置。
这里是xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.TextInputLayout
android:id="@+id/til_relationship"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.TextInputEditText
android:id="@+id/et_temp_relationship"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:hint="Gender"
android:longClickable="false" />
</android.support.design.widget.TextInputLayout>
<Spinner
android:id="@+id/relationWithBaby"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:nextFocusDown="@+id/gender"
android:paddingBottom="14dp"
android:paddingLeft="9dp"
android:paddingRight="9dp"
android:paddingTop="25dp"
app:layout_constraintBottom_toBottomOf="@+id/til_relationship"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/til_relationship" />
</android.support.constraint.ConstraintLayout>
</layout>
这是我的 activity 绑定代码(单向)
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private ActivityMainBinding mMainBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
initUI();
}
private void initUI() {
mMainBinding.relationWithBaby.setAdapter(new MyArrayAdapter(this, R.layout.custom_layout, R.id.custom_text, addEmptyElementToArray(getResources().getStringArray(R.array.user_gender))));
mMainBinding.relationWithBaby.setOnItemSelectedListener(this);
mMainBinding.relationWithBaby.setSelection(0);
}
private String[] addEmptyElementToArray(String[] array) {
List<String> list = new ArrayList<>(Arrays.asList(array));
list.add(0, "");
return (list.toArray(new String[list.size()]));
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
private class MyArrayAdapter extends ArrayAdapter<String> {
private int textViewResourceId;
private int customText;
public MyArrayAdapter(Context context, int textViewResourceId, int custom_text, String[] objects) {
super(context, textViewResourceId, objects);
this.textViewResourceId = textViewResourceId;
this.customText= custom_text;
}
@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view;
TextView textSpinnerName;
if (convertView == null) {
view = getLayoutInflater().inflate(textViewResourceId, parent, false);
} else {
view = convertView;
}
textSpinnerName = (TextView) view.findViewById(R.id.custom_text);
String item;
item = getItem(position);
textSpinnerName.setText(item);
return view;
}
}
}
这是我正在膨胀的自定义视图
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/custom_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
tools:text="Apple" />
</android.support.constraint.ConstraintLayout>
我知道我应该在适配器设置中提供 textview 的另一个参数,但不明白为什么它找不到该视图的 ID
对于那些想在 android 中的文本输入布局中使用微调器的人来说,这里是代码。
- 保持 XML 主文件不变 XML
- 从自定义微调器中删除约束布局XML
- 将此代码添加到您的 activity/fragment
适配器设置代码
mUserBinding.spinnerProfileGender.setAdapter(new MyArrayAdapter(getActivity(), R.layout.custom_spinner, addEmptyElementToArray(getResources().getStringArray(R.array.user_gender))));
mUserBinding.spinnerProfileGender.setOnItemSelectedListener(this);
mUserBinding.spinnerProfileGender.setSelection(0);
适配器代码
userCountryAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_dropdown_item, userCountry);
mUserBinding.spinnerProfileState.setAdapter(userCountryAdapter);
And here is final code for activity/fragment
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
for (int j = 0; j < parent.getChildCount(); j++) {
parent.getChildAt(0);
}
if (parent == mUserBinding.spinnerProfileGender) {
if (position != 0) {
mUserBinding.inputTextEditGender.setText(" ");
//TODO NOTE
//For sending selection to upload data used spinner data
Toast.makeText(getActivity(), " " + mUserBinding.spinnerProfileGender.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
}
} else if (parent == mUserBinding.spinnerProfileState) {
if (position != 0) {
mUserBinding.inputtextProfileState.setText(userCountry.get(position));
}
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
private class MyArrayAdapter extends ArrayAdapter<String> {
private int textViewResourceId;
public MyArrayAdapter(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
this.textViewResourceId = textViewResourceId;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view;
TextView text;
if (convertView == null) {
view = getLayoutInflater().inflate(textViewResourceId, parent, false);
} else {
view = convertView;
}
text = (TextView) view;
String item;
item = getItem(position);
text.setText((CharSequence) item);
if (position == 0) {
ViewGroup.LayoutParams l = text.getLayoutParams();
l.height = 1;
text.setLayoutParams(l);
} else {
ViewGroup.LayoutParams l = text.getLayoutParams();
//you can change height value to yours
l.height = 150;
text.setLayoutParams(l);
}
return view;
}
}
@NonNull
private String[] addEmptyElementToArray(String[] array) {
List<String> list = new ArrayList<>(Arrays.asList(array));
list.add(0, "");
return (list.toArray(new String[list.size()]));
}
在第三方微调库提供的 android 文本输入布局中设置微调只是一个小技巧。
并且在 Onitemselection 中,您可以从微调器位置而不是编辑文本中获取数据