从CursorLoader填充的Android Spinner不显示所选项

时间:2018-02-21 20:45:30

标签: android spinner android-cursorloader

我在一个片段中有2个微调器。从List填充的第二个显示数字1到100以及我选择它后的值。

第一个微调器从CursorLoader中填充。当我单击微调器时,它会显示正确的主题列表,但是当我选择主题时,不会出现此选定的主题。 (我已经在屏幕截图中显示了我的意思)。

我的片段代码和片段布局如下:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView =  inflater.inflate(R.layout.fragment_user_input, container, false);

        subjectSpinner = rootView.findViewById(R.id.subjects_spinner);
        subjectAdapter = new SimpleCursorAdapter(
                getActivity(),
                R.layout.support_simple_spinner_dropdown_item,
                null,
                new String[]{ProgrammeCourseContract.SubjectsEntry.COLUMN_TITLE},
                new int[]{R.id.subject_spinner_name});

        subjectAdapter.setDropDownViewResource(R.layout.subject_spinner_item);
        subjectAdapter.notifyDataSetChanged();
        subjectSpinner.setAdapter(subjectAdapter);

        marksSpinner = rootView.findViewById(R.id.marks_spinner);
        marksAdapter = new ArrayAdapter<Integer>(
                getActivity(),
                R.layout.support_simple_spinner_dropdown_item,
                getMarks()
        );

        marksAdapter.setDropDownViewResource(R.layout.mark_spinner_item);
        marksSpinner.setAdapter(marksAdapter);

        return rootView;

    }

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/user_input_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.wernerraubenheimer.ujcp.fragments.UserInputFragment">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:orientation="vertical">
        <!-- Subjects Spinner Dropdown -->
        <Spinner
            android:id="@+id/subjects_spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="20dp"
            android:spinnerMode="dropdown"
            android:background="@android:color/holo_orange_light"
            android:prompt="@string/subject_spinner_prompt" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">
        <!-- Marks Spinner Dropdown -->
        <Spinner
            android:id="@+id/marks_spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="20dp"
            android:spinnerMode="dropdown"
            android:background="@android:color/holo_orange_light"
            android:prompt="@string/subject_spinner_prompt" />
    </LinearLayout>

</LinearLayout>

mark_spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mark_spinner_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@android:color/black"
    android:textSize="16sp"
    android:gravity="left"
    android:padding="5dp"/>

subject_spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/subject_spinner_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@android:color/black"
    android:textSize="16sp"
    android:gravity="left"
    android:padding="5dp"/>

CursorLoader works - list of subjects show

Selected subject doesn't show

2 个答案:

答案 0 :(得分:0)

修复了SimpleCursorAdapter。确保我指定的TextView ID与那里引用的布局相同。确保引用包含TextView的相同布局文件。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    rootView =  inflater.inflate(R.layout.fragment_user_input, container, false);

    subjectSpinner = rootView.findViewById(R.id.subjects_spinner);
    subjectAdapter = new SimpleCursorAdapter(
            getActivity(),
            R.layout.subject_spinner_item,
            null,
            new String[]{ProgrammeCourseContract.SubjectsEntry.COLUMN_TITLE},
            new int[]{R.id.subject_spinner_name});

    subjectAdapter.notifyDataSetChanged();
    subjectSpinner.setAdapter(subjectAdapter);


    marksSpinner = rootView.findViewById(R.id.marks_spinner);
    marksAdapter = new ArrayAdapter<Integer>(
            getActivity(),
            R.layout.support_simple_spinner_dropdown_item,
            getMarks()
    );

    marksAdapter.setDropDownViewResource(R.layout.mark_spinner_item);
    marksSpinner.setAdapter(marksAdapter);

    return rootView;

}

答案 1 :(得分:0)

来自docs:

  

[SimpleCursorAdapter]是一个简单的适配器,用于将游标中的列映射到TextViews

及其构造函数

root.Traverse(node =>
{
    var depth = 0;
    var ancestor = node.Parent;
    while (ancestor != null)
    {
        depth++;
        ancestor = ancestor.Parent;
    }
    Console.WriteLine(new string(' ', depth * 2) + node.Value.Name);
});

(我认为你正在使用一个已弃用的)但我无法得到你所映射到的东西。此外,我在您的布局中看不到SimpleCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to, int flags) ,而文档声明:

  

to int:应在“from”参数中显示列的视图。   这些都应该是TextViews。此列表中的前N个视图是   给出from参数中前N列的值。可   如果光标尚不可用,则返回null。

对我来说,看起来你应该只使用另一个ArrayListAdapter,因为最终的目的与右旋转器的类似。我是对的吗?