2017-04-03 64 views
1

我遇到了spinner問題,它不允許我選擇一個項目。我嘗試了很多東西,但仍然無法正常工作。Android Custom Spinner無法選擇一個選項並顯示它

圖爲微調是空白的活動負載Image-1

當我點擊箭頭它顯示的項目[Image-2]

但是當我選擇一個,沒有什麼happends。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 

    tools:context=".Activities.Inspeccion.DatosGeneralesActivity"> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/scrollView" 
     > 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="15dp" 
      android:layout_marginRight="15dp" 
      android:layout_marginTop="15dp" 
      > 
      <TextView 
       android:id="@+id/tvSubestacionTitulo" 
       android:layout_below="@+id/imgLogo" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/strSubestacion" 
       android:textSize="18sp" 
       android:textColor="@color/colorPrimaryDark" 
       android:textStyle="bold" 
       /> 
      <TextView 
       android:id="@+id/tvSubestacionDato" 
       android:layout_below="@+id/tvSubestacionTitulo" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="12sp" 
       android:text="Prueba" 
       /> 
      <Spinner 
       android:id="@+id/spinnerSubEstacion" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/tvSubestacionDato" 
       > 
      </Spinner> 
     </RelativeLayout>   
    </ScrollView> 
</RelativeLayout> 

這是活動的佈局。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <TextView 
     android:id="@+id/tvNumeroOpcion" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_marginLeft="15dp" 
     android:layout_marginTop="5dp" 
     android:text="1" 
     android:textColor="@color/black" 
     android:textSize="14sp" /> 

    <TextView 
     android:id="@+id/tvDescriptionOption" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_marginLeft="15dp" 
     android:layout_marginTop="5dp" 
     android:text="Guatemala" 
     android:textColor="@color/black" 
     android:textSize="14sp" /> 
</LinearLayout> 

這是對微調

Public class ComboAdapter extends BaseAdapter{ 


    private List<Combo> combos; 
    private Activity activity; 
    private LayoutInflater inflater; 

    public ComboAdapter(List<Combo> combos, Activity activity) { 

     this.combos = combos; 
     this.inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public int getCount() { 
     return combos.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = convertView; 

     if (convertView == null){ 
      view = inflater.inflate(R.layout.combo_list_item, null); 

      TextView tvId = (TextView) view.findViewById(R.id.tvNumeroOpcion); 
      TextView tvDescripcion = (TextView) view.findViewById(R.id.tvDescriptionOption); 

      tvId.setText(combos.get(position).getId()); 
      tvDescripcion.setText(combos.get(position).getDescripcion()); 
     } 
       return view; 
    } 

    @Override 
    public View getDropDownView(int position, View convertView, 
           ViewGroup parent) { 
     return getView(position, convertView,parent); 
    } 
} 

這是我的適配器

而下面是我的活動自定義佈局。

public class DatosGeneralesActivity extends AppCompatActivity { 

    private TextView tvSubestacionDato; 

    private List<Combo> listaCombo; 

    private Spinner spinnerSubestacion; 

    private ArrayAdapter<Combo> adapterSubestacion; 

    String seleccion; 

    @Override 
    protected void onCreate(final Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_datos_generales); 
     //Inicializando textos 
     tvSubestacionDato = (TextView) findViewById(R.id.tvSubestacionDato); 

     //Inicializanco listas 
     listaCombo = new ArrayList<>(); 

     //Inivializando spinners 
     spinnerSubestacion = (Spinner) findViewById(R.id.spinnerSubEstacion); 


     AppService service = API.getCombos().create(AppService.class); 
     Call<List<Combo>> subestacionCall = service.getSubestacion(); 

     subestacionCall.enqueue(new Callback<List<Combo>>() { 
      @Override 
      public void onResponse(Call<List<Combo>> call, Response<List<Combo>> response) { 
       listaCombo.clear(); 
       listaCombo.addAll(response.body()); 
      } 

      @Override 
      public void onFailure(Call<List<Combo>> call, Throwable t) { 

      } 
     }); 

     //final ComboAdapter adapter = new ComboAdapter(listaCombo, DatosGeneralesActivity.this); 
     final ArrayAdapter<Combo> adapter = new ArrayAdapter<Combo>(this, R.layout.support_simple_spinner_dropdown_item, listaCombo); 
     spinnerSubestacion.setAdapter(adapter); 

     spinnerSubestacion.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       adapter.notifyDataSetChanged(); 
       Toast.makeText(DatosGeneralesActivity.this, ""+position, Toast.LENGTH_SHORT).show(); 
       tvSubestacionDato.setText(listaCombo.get(position).getDescripcion()); 
      } 

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

      } 
     }); 
    } 
+0

你爲什麼要調用'adapter.notifyDataSetChanged()':???? – AwaisMajeed

+0

因爲我在互聯網上看到了,而且我很絕望,所以我試圖補充說明,看看它是否有效。 –

+0

你不需要,因爲你沒有更新listaCombo – AwaisMajeed

回答

0

試試這個變化:

呼叫轉接器,如:

適配器類
ComboAdapter adapter = new ComboAdapter(DatosGeneralesActivity.this, 
      R.layout.combo_list_item, R.id.tvDescriptionOption, listaCombo); 

現在:

public ComboAdapter(Activity context,int resouceId, int textviewId, List<Combo> list){ 

     super(context,resouceId,textviewId, list); 
     this.combos = list; 
     this.inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

而且你getView()方法內部膨脹的佈局,如:

if (convertView == null){ 
    view = inflater.inflate(R.layout.combo_list_item, parent , false); 
+0

Android工作室不讓我使用:super(context,resouceId,textviewId,list);表示父母期望0參數。如果我不使用它們導致我錯誤:java.lang.NullPointerException:嘗試在空對象引用和java上調用虛擬方法'java.lang.Object android.app.Activity.getSystemService(java.lang.String)' .lang.Object android.app.Activity.getSystemService(java.lang.String)'null對象引用 –

+0

使用'公共類ComboAdapter擴展ArrayAdapter '...而不是baseAdapter – rafsanahmad007

相關問題