2017-07-19 45 views
0

我已經創建了一個應用程序,這是從一個WebService充入數據的個性化列表,當你看到它的領域之一是國家,但是這將返回的數量( 0,1,2)是我想要做的就是把它放在「VACANTE」,「被佔領」,「保留」,並且通過開關來實現,但我不知道如何使用它將一個開關在viewHolder - 自定義列表Android Studio中

viewHolder.estado.setText(items.get(i).getEstado());//number (0,1,2) 

這個數據我把它從一個PHP WebService這是我目前的代碼:

public class MetaAdapter extends RecyclerView.Adapter<MetaAdapter.MetaViewHolder> 
     implements ItemClickListener { 

    /** 
    * Lista de objetos {@link Meta} que representan la fuente de datos 
    * de inflado 
    */ 
    private List<Plaza> items; 

    /* 
    Contexto donde actua el recycler view 
    */ 
    private Context context; 


    public MetaAdapter(List<Plaza> items, Context context) { 
     this.context = context; 
     this.items = items; 
    } 

    @Override 
    public int getItemCount() { 
     return items.size(); 
    } 

    @Override 
    public MetaViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
     View v = LayoutInflater.from(viewGroup.getContext()) 
       .inflate(R.layout.item_list, viewGroup, false); 
     return new MetaViewHolder(v, this); 
    } 

    @Override 
    public void onBindViewHolder(MetaViewHolder viewHolder, int i) { 
     viewHolder.nivel.setText(items.get(i).getNivel()); 
     viewHolder.estado.setText(items.get(i).getEstado()); 
     viewHolder.colegio.setText(items.get(i).getIe()); 
     viewHolder.especialidad.setText(items.get(i).getEspecialidad()); 
     viewHolder.codigo.setText(items.get(i).getCodigoPlaza()); 
    } 


    /** 
    * Sobrescritura del método de la interfaz {@link ItemClickListener} 
    * 
    * @param view  item actual 
    * @param position posición del item actual 
    */ 
    @Override 
    public void onItemClick(View view, int position) { 
     DetailActivity.launch(
       (Activity) context, items.get(position).getPlazaID()); 
    } 


    public static class MetaViewHolder extends RecyclerView.ViewHolder 
      implements View.OnClickListener { 
     // Campos respectivos de un item 
     public TextView nivel; 
     public TextView estado; 
     public TextView colegio; 
     public TextView especialidad; 
     public TextView codigo; 
     public ItemClickListener listener; 


     public MetaViewHolder(View v, ItemClickListener listener) { 
      super(v); 
      nivel = (TextView) v.findViewById(R.id.nivel); 
      estado = (TextView) v.findViewById(R.id.estado); 
      colegio = (TextView) v.findViewById(R.id.colegio); 
      especialidad = (TextView) v.findViewById(R.id.especialidad); 
      codigo = (TextView) v.findViewById(R.id.codigo); 
      this.listener = listener; 
      v.setOnClickListener(this); 
     } 

     @Override 
     public void onClick(View v) { 
      listener.onItemClick(v, getAdapterPosition()); 
     } 
    } 
} 


    interface ItemClickListener { 
     void onItemClick(View view, int position); 
} 

目前在我的應用程序中看起來像這樣: enter image description here

+0

你只是想顯示狀態'String'(如「VACANTE」,而不是「0」),或者你想添加一個GUI元素,讓你的狀態切換爲每一個列表項? – deHaar

+0

只需更換爲字符串(如「VACANTE」而不是「0」) –

回答

1
@Override 
    public void onBindViewHolder(MetaViewHolder viewHolder, int i) { 
     switch (items.get(i).getEstado()) { 
     case "0": 
      return viewHolder.estado.setText("VACANTE"); 
     case "1": 
      return viewHolder.estado.setText("OCCUPIED"); 
     case "2": 
      return viewHolder.estado.setText("RESERVED"); 
     default: 
      return viewHolder.estado.setText(""); 
    } 

    } 
+0

非常感謝你 –

1

然後,你需要申請一個字符串的每一個國家,例如

public String getStateAsString(int state) { 
    switch (state) { 
     case 0: 
      return "VACANTE"; 
     case 1: 
      return "OCCUPIED"; 
     case 2: 
      return "RESERVED"; 
     default: 
      return "STATE NOT DEFINED"; 
    } 
} 

,並調用該方法,而不是直接分配狀態int你的TextView:

/* 
* Instead of 
* viewHolder.codigo.setText(items.get(i).getCodigoPlaza()); 
* you do the following 
*/ 
int stateAsInt = items.get(i).getCodigoPlaza(); 
String stateAsString = getStateAsString(stateAsInt); 
viewHolder.estado.setText(state);