spinner:当ui(spinner)包含在主xml文件中时,数组适配器不工作

vzgqcmou  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(148)

我有一个带有微调器的ui和一些使用arraydapter的自定义字符串数组项。
如果在activity\u main中添加了微调器项,则一切正常。
但是如果我在一个单独的布局中添加微调器,并将其包含在activity\u main中,那么相同的代码不会填充微调器。
我使用layoutinflater扩展了视图,在日志中没有发现异常。
有谁能告诉我如何将arrayadapter从java类添加到添加了的ui中。
我试过试过了。
工作代码
主活动.java

package com.abhiroop.simplespinnerappcustom;

   public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

// array of Strings
String[] courses = {"C", "Data structures",
        "Interview prep", "Algorithms",
        "DSA with java", "OS"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Spinner spino = findViewById(R.id.coursesspinner);
    spino.setOnItemSelectedListener(this);

    ArrayAdapter ad
            = new ArrayAdapter(
            this,
            android.R.layout.simple_spinner_item,
            courses);

    ad.setDropDownViewResource(
            android.R.layout
                    .simple_spinner_dropdown_item);
    spino.setAdapter(ad);
}

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}

}
活动\u main.xml

<?xml version="1.0" encoding="utf-8"?>
<!--Constraint layout which contain Spinner widget-->
 <androidx.constraintlayout.widget.ConstraintLayout
    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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<!--Spinner widget-->
<Spinner
    android:id="@+id/coursesspinner"
    android:layout_height="50dp"
    android:layout_width="160dp"
    android:layout_marginEnd="10dp"
    android:layout_marginStart="10dp"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="10dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

无工作代码
mainactivity.java“”包com.abhiroop.simplespinnerappcustom;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

String[] courses = { "C", "Data structures",
        "Interview prep", "Algorithms",
        "DSA with java", "OS" };

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     View mainView = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
    View view = LayoutInflater.from(this).inflate(R.layout.spinner_layout, (ViewGroup) mainView, true);
    Spinner spino = view.findViewById(R.id.coursesspinner);
    spino.setOnItemSelectedListener(this);

    ArrayAdapter ad
            = new ArrayAdapter(
            this,
            android.R.layout.simple_spinner_item,
            courses);

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

    spino.setAdapter(ad);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}

} '''
活动\u main.xml“”

<?xml version="1.0" encoding="utf-8"?>
    <!--Constraint layout which contain Spinner widget-->
        <androidx.constraintlayout.widget.ConstraintLayout
        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:id="@+id/main"
        android:layout_width="match_parent"
android:layout_height="match_parent">

<!--Spinner widget-->
<!--<Spinner
    android:id="@+id/coursesspinner"
    android:layout_height="50dp"
    android:layout_width="160dp"
    android:layout_marginEnd="10dp"
    android:layout_marginStart="10dp"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="10dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>-->

<include
    android:id="@+id/spinner_layout"
    layout="@layout/spinner_layout"/>

</androidx.constraintlayout.widget.constraintlayout>“”
微调器布局.xml

<?xml version="1.0" encoding="utf-8"?>
    <Spinner 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:id="@+id/coursesspinner"
    android:layout_width="160dp"
    android:layout_height="50dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="10dp"
    android:layout_marginBottom="10dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题