2017-08-14 132 views
0

我已经从类别类的服务器获取数据。getcategories方法返回包含微调项目的字符串列表。当我点击微调项目。什么都没发生。我的代码中是否有任何错误。请帮忙。当我点击Spinner项目时没有任何反应

这是我的java代码。

public void fetchPropertyType(){ 
     category = new Category(); //Spinner Item model 
     //categories is a array list of String which contains the items of spinner 
     categories = category.getCategories(AddPropertyActivity.this); 

     //Property Type Spinner Adapter 
     propertyTypeSpinner = (Spinner) findViewById(R.id.property_type_spinner); 
     Log.e("Test", "Just a test message"); 

     ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories); 
     // Drop down layout style - list view with radio button 
     dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     // attaching data adapter to spinner 
     propertyTypeSpinner.setAdapter(dataAdapter); 

     propertyTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       Toast.makeText(parent.getContext(), 
         "OnItemSelectedListener : " + parent.getItemAtPosition(position).toString(), 
         Toast.LENGTH_SHORT).show(); 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 
       Log.e("Test", "Nothing selected on spinner activity"); 

      } 
     }); 
    } 

这是我的布局

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginLeft="5dp" 
    android:layout_weight="1"> 

    <TextView 
     android:id="@+id/spinner_text" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginRight="50dp" 
     android:gravity="center" 
     android:text="Property Type" 
     android:textAlignment="textEnd"/> 

    <Spinner 
     android:id="@+id/property_type_spinner" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:spinnerMode="dropdown"/> 

</RelativeLayout> 
+0

嗨Xantosh Lamsal,你可以查看我的答案。我在代码中使用它是好的。你可以试试。 – KeLiuyue

回答

1

你刚才应该这样做。

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginLeft="5dp" 
    android:layout_weight="1" 
    android:background="@drawable/border"> 

    <TextView 
     android:id="@+id/spinner_text" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginRight="50dp" 
     android:gravity="center" 
     android:text="Property Type" 
     android:textAlignment="textEnd"/> 

    <Spinner 
     android:id="@+id/property_type_spinner" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:spinnerMode="dropdown"/> 

</RelativeLayout> 

在微调

变化

android:layout_height="match_parent" 

android:layout_height="wrap_content" 

因为你使用android:layout_height="match_parent",所以你看不到你的清单item.And没有反应。

1

当您使用

<Spinner 
     android:id="@+id/property_type_spinner" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:spinnerMode="dropdown"/> 

它包装微调的高度,所以尽量给定制的高度,以便它可点击

我这样做

<Spinner 
     android:id="@+id/property_type_spinner" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
    android:spinnerMode="dropdown"/> 

和保持文本视图高度wrap_content为

<TextView 
     android:id="@+id/spinner_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="50dp" 
     android:gravity="center" 
     android:text="Property Type"/> 
相关问题