2017-08-14 55 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"/> 
相關問題