ArrayAdapter要求资源ID为用于数据绑定和ConstraintLayout的TextView

时间:2018-07-19 14:53:47

标签: android android-arrayadapter android-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>

这是我的绑定活动代码(单向)

    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

1 个答案:

答案 0 :(得分:1)

对于那些想要在android的文本输入布局中使用微调器的人,这里的代码用来跟踪新的代码。

  • 保留用于主要XML的XML文件
  • 从自定义微调器XML中删除约束布局
  • 将此代码添加到您的活动/片段中

适配器设置代码

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中,您可以从微调器位置而不是Edittext抓取数据