2017-08-04 50 views
0

我有一個包含從Web服務項的微調..微調的項目在下拉列表中不可見

this is the image for spinner

這個代碼是

JsonArrayRequest fillspinner = new JsonArrayRequest(GET_SPINNER_ITEM, new Response.Listener<JSONArray>() { 
     @Override 
     public void onResponse(JSONArray response) { 

      try{ 
      for(int i =0;i<=response.length();i++){ 
       JSONObject obj = response.getJSONObject(i); 
       names = obj.getString("Name"); 
       itemnames.add(names); 
      } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      ArrayAdapter<String> adap = new ArrayAdapter<>(getApplicationContext(),android.R.layout.simple_spinner_item,itemnames); 
      spinner.setAdapter(adap); 
      adap.notifyDataSetChanged(); 


     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 

     } 
    }); 

    fillspinner.setRetryPolicy(new DefaultRetryPolicy(
      30000, 
      DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
      DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 
    VolleyApplication.getInstance().getRequestQueue().add(fillspinner); 
} 

和我layout.xml是

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:id="@+id/drawer_layout" 
tools:context=".ActivityClasses.MainActivity" 
android:fitsSystemWindows="true"> 

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

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:layout_margin="@dimen/welbtnmargin"> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/ripple" 
     android:textColor="@color/white" 
     android:id="@+id/date" 
     android:layout_marginBottom="@dimen/btnsepration" 
     android:text="-"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Expenses" 
     android:gravity="center" 
     android:layout_marginBottom="@dimen/btnsepration" 
     android:layout_gravity="center"/> 

    <com.jaredrummler.materialspinner.MaterialSpinner 
     android:id="@+id/spinner" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:ms_arrow_tint="@color/white" 
     app:ms_text_color="@color/white" 
     app:ms_background_color="@color/colorPrimaryDark"/> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/header_height" 
     android:elevation="4dp" 
     /> 

</LinearLayout> 

我的問題是當我點擊微調,我得到這樣的輸出。

there are items in the spinner but they are not visible

當我點擊的項目它的微調得到選擇。但在下拉列表中不可見。我試圖在適配器中膨脹我自己的佈局,仍然得到相同的結果。

+0

我覺得你的代碼是OK,再次檢查JSON陣列響應... – Omi

+0

你檢查,在JSON響應'Name' attr爲不爲空字符串? – chandil03

+0

我檢查了五次。它的未來正確 –

回答

0

您正在使用第三方微調器。它需要以特定的方式插入項目。

String[] array = itemnames.toArray(new String[]{}); 
spinner.setItems(array); 

https://github.com/jaredrummler/MaterialSpinner

+0

此方法適用於我。我剛剛刪除了適配器並設置了上面的代碼..現在顯示...謝謝 –

-1

使用下面的代碼;

for (int i = 1; i < cityname.length(); i++) { 
     try 
     { 
      JSONObject jsonObject = cityname.getJSONObject(i); 
      //Adding the name of the Rank to array list 
      country.add(jsonObject.getString("name")); 
      autotrno.add(jsonObject.getString(Config.TAG_RANK)); 
     } 
     catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    }  
    ArrayAdapter<String> adap = new ArrayAdapter<>(getApplicationContext(),android.R.layout.simple_spinner_item,country); 
     spinner.setAdapter(adap); 
+0

你能解釋這個代碼與OP有什麼不同嗎? –

相關問題