2017-08-04 67 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有什么不同吗? –