下拉列表不会出现

时间:2017-11-02 05:40:15

标签: android android-spinner

[在此处输入图片说明] 1 [在此处输入图片说明] 2 [在此处输入图片说明] 3我一直试图制作一个下拉框,但我无法看到下拉列表。仅显示第一个选项。出现第一个选项,然后关闭应用程序。我是Android新手请帮助。这是主要的xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="Category:"
        android:layout_marginBottom="5dp"/>

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/spinner_title"/>

</LinearLayout>

和主要活动

public class MainActivity extends AppCompatActivity implements OnItemSelectedListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Spinner spinner = (Spinner) findViewById(R.id.spinner);

    // Spinner click listener
    spinner.setOnItemSelectedListener(this);
    List<String> categories = new ArrayList<String>();
    categories.add("Auto");
    categories.add("BUI");
 ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner.setAdapter(dataAdapter);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    // On selecting a spinner item
    String item = parent.getItemAtPosition(position).toString();

    // Showing selected spinner item
    Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
}

}

我的gradle文件

 apply plugin: 'com.android.application'


android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
    applicationId "com.example.admin.mydropdowns"
    minSdkVersion 16
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

}


  dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'

 }

这是我的logcat

11-03 10:29:47.260 3721-3721/? I/art: Not late-enabling -Xcheck:jni (already on)

11-03 10:29:47.260 3721-3721/? W/art: Unexpected CPU variant for X86 using defaults: x86

11-03 10:29:47.404 3721-3721/com.example.admin.mydropdowns W/System: ClassLoader referenced unknown path: /data/app/com.example.admin.mydropdowns-2/lib/x86

 11-03 10:29:47.428 3721-3721/com.example.admin.mydropdowns W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable


   [ 11-03 10:29:47.467  1525: 1573 D/         ]

HostConnection::get() New Host Connection established 0x90ccdf40, tid 1573

11-03 10:29:47.600 3721-3741/com.example.admin.mydropdowns I/OpenGLRenderer: Initialized EGL, version 1.4

11-03 10:29:47.600 3721-3741/com.example.admin.mydropdowns D/OpenGLRenderer: Swap behavior 1


11-03 10:29:47.601 3721-3741/com.example.admin.mydropdowns W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...

11-03 10:29:47.601 3721-3741/com.example.admin.mydropdowns D/OpenGLRenderer: Swap behavior 0

11-03 10:30:00.491 3721-3741/com.example.admin.mydropdowns E/OpenGLRenderer: GL error: 0x506

 11-03 10:30:00.492 3721-3741/com.example.admin.mydropdowns A/OpenGLRenderer: GL errors! frameworks/base/libs/hwui/renderthread/CanvasContext.cpp:550

 11-03 10:30:00.492 3721-3741/com.example.admin.mydropdowns A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 3741 (RenderThread)


    [ 11-03 10:30:00.492  1222: 1222 W/         ]
                                                               debuggerd: handling request: pid=3721 uid=10076 gid=10076 tid=3741

2 个答案:

答案 0 :(得分:0)

删除该行:

android:prompt="@string/spinner_title"
<布局文件

中的

除此之外,此代码段中的工作方式绝对正常。

然后将你的arraylist设为

List<String> categories = new ArrayList<String>(); 
categories.add("Select Category"); 
categories.add("Auto"); 
categories.add("BUI"); 

然后根据需要按索引位置处理条件

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position ==0){
 // Showing the prompt
    Toast.makeText(parent.getContext(), "Please select category", Toast.LENGTH_LONG).show();
}else{
 // On selecting a spinner item
 String item = parent.getItemAtPosition(position).toString();

 // Showing selected spinner item
 Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();}}

答案 1 :(得分:0)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="Category:"
    android:layout_marginBottom="5dp"/>

<Spinner
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

</LinearLayout>

尝试将ur xml文件更改为此文件 然后将您的java文件的代码更改为: 因为当你展示吐司时你没有给出正确的上下文,你需要给出你在其中而不是适配器视图的活动的上下文。

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Spinner spinner = (Spinner) findViewById(R.id.spinner);

    List<String> categories = new ArrayList<String>();
    categories.add("Auto");
    categories.add("BUI");
 ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, categories);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner.setAdapter(dataAdapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> dataAdapter, View v, int position, long id)
            {
               String item = dataAdapter.getItemAtPosition(position).toString();

               // Showing selected spinner item
               Toast.makeText(getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();

            }

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

            }
        });
}

}

这里有输出: enter image description here

enter image description here

enter image description here

enter image description here