如何获得所选微调器项目的值并将其设置为TextView?

问题描述 投票:0回答:1

好的,想象一下。我在页面中动态添加和减少微调器,它们中都有2​​50多个项目,它们是从ArrayList类型的对象的Course对象中获得的,而另一方面ArrayListTreeMap中获取其值],因为我需要对其进行排序。现在的问题。我希望有一个小按钮“关于课程”,单击该按钮时,它将显示当前所选课程的弹出窗口,并将视图设置为适当的值(我具有CRN,名称,描述和学分视图)。那么,如何指定要检索微调器上选定对象的对象信息,然后将TextView值设置为该对象呢? Switch语句不是一种选择,因为我在这里处理大量对象。我正在尝试rn

CRN.setText(courseMap.get(courseSpinner.getSelectedItem()).getCRN());

但是我遇到了崩溃java.lang.ClassCastException: coda759.schedulebuilder2.Course cannot be cast to java.lang.Comparable也尝试过此

CRN.setText(courseMap.get(String.valueOf(courseSpinner.getSelectedItem())).getCRN());

我得到java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

方法:

public void aboutCoursePopup(View v) {
        TextView CRN;
        TextView name;
        TextView credits;
        TextView description;
        aboutCourse.setContentView(R.layout.about_course_popup);
        CRN = findViewById(R.id.crn_popup);
        name = findViewById(R.id.name_popup);
        credits = findViewById(R.id.credits_popup);
        description = findViewById(R.id.desc_popup);
        CRN.setText(courseMap.get(String.valueOf(courseSpinner.getSelectedItem())).getCRN());
        aboutCourse.show();
//following commented out also causes a crash
//        Button btnOk = findViewById(R.id.about_ok);
//        btnOk.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                aboutCourse.dismiss();
//            }
//        });
    }
public void aboutCoursePopup(View v) {
        TextView CRN;
        TextView name;
        TextView credits;
        TextView description;
        aboutCourse.setContentView(R.layout.about_course_popup);
        CRN = findViewById(R.id.crn_popup);
        name = findViewById(R.id.name_popup);
        credits = findViewById(R.id.credits_popup);
        description = findViewById(R.id.desc_popup);
        //need to setText of CRN, name, description and credits here
        aboutCourse.show();
    }

此位在onCreate()方法中

aboutCourse = new Dialog(this);
        //about course btn
        Button abtCourseBtn = findViewById(R.id.about_course_btn);
        abtCourseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                aboutCoursePopup(v);
            }
        });

这里是弹出的xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="400dp"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:background="@color/colorSecondaryDark"
    android:layout_gravity="center">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@color/colorPrimary">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="CRN:"
            android:textSize="25sp" />

        <TextView
            android:id="@+id/crn_popup"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            android:text="PLHXXX"
            android:textSize="25sp"
            android:layout_marginLeft="38dp"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@color/colorPrimaryDark">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Name:"
            android:textSize="25sp"/>

        <TextView
            android:id="@+id/name_popup"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:text="Course Name"
            android:textSize="25sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@color/colorPrimary">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Credits:"
            android:textSize="25sp"/>

        <TextView
            android:id="@+id/credits_popup"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:text="X.0"
            android:textSize="25sp"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@color/colorPrimaryDark">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="Description:"
            android:textSize="25sp"/>

        <TextView
            android:id="@+id/desc_popup"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
            android:textSize="20sp"
            android:padding="4dp"/>

    </LinearLayout>

    <Button
        android:id="@+id/about_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OK"
        android:background="@color/colorPrimary" />
</LinearLayout>
java android spinner
1个回答
0
投票

adapter = new ArrayAdapter(getApplicationContext(),android.R.layout.simple_spinner_item,英雄);categoryspin.setAdapter(adapter);

            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

            int selectedItemOfMySpinner = categoryspin.getSelectedItemPosition();
            String actualPositionOfMySpinner = (String) categoryspin.getItemAtPosition(selectedItemOfMySpinner);

            if (actualPositionOfMySpinner.isEmpty()) {

                alertDialogueBox.setSpinnerError(categoryspin, getString(0));
            }
            categoryspin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                public void onItemSelected(AdapterView<?> parent, View view, final int pos, long id) {

                    if (pos >= 0 && pos < catheroList.size()) {
                        item = catheroList.get(pos).getId();
                        Log.e("item", item);
                    } else {
                        Toast.makeText(MainActivity.this, "Selected Category Does not Exist!", Toast.LENGTH_SHORT).show();
                    }
© www.soinside.com 2019 - 2024. All rights reserved.