2012-07-14 83 views
2

我有一個ListView,當我點擊一個按鈕,我想添加數據到我的ListView。 我有一個自定義的ArrayAdapter爲此,但這隻適用於第一次點擊,第二不添加任何東西,但如果我調試數據它沒關係。動態添加一行ListView

代碼:

public class gremioAdapter extends ArrayAdapter<Gremio> { 

     Context context; 
     int layoutResourceId; 
     ArrayList<Gremio> data = null; 

     public gremioAdapter(Context context, int layoutResourceId, ArrayList<Gremio> data) 
     { 
      super(context, layoutResourceId, data); 
      this.layoutResourceId = layoutResourceId; 
      this.context = context; 
      this.data = data; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View row = convertView; 
      GremioHolder holder = null; 

      if (row == null) { 
       LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
       row = inflater.inflate(layoutResourceId, parent, false); 

       holder = new GremioHolder(); 
       holder.tvGremio = (TextView) row.findViewById(R.id.tvGremio); 
       holder.etComentario = (EditText) row.findViewById(R.id.etComentario); 
       holder.cbActivo = (CheckBox) row.findViewById(R.id.cbGremioActivo); 
       row.setTag(holder); 
      } 
      else { 
       holder = (GremioHolder) row.getTag(); 
      } 

      Gremio gremio = data.get(position); 
      holder.tvGremio.setText(gremio.literal); 
      holder.etComentario.setText(gremio.comentario);  
      posicion++; 


      return row; 
     } 

     public class GremioHolder { 
      TextView tvGremio; 
      EditText etComentario; 
      CheckBox cbActivo; 
     } 
    } 

} 

我這裏補充數據:

btnAnadirGremio.setOnClickListener(new OnClickListener() 
     { 

      @Override 
      public void onClick(View v) { 

       gremioQueQuiereAnadir = spinnerGremios.getSelectedItem().toString(); 
       codigoDelGremio = respuestaTerminar.getListaGremiosDisponibles().get(gremioClickeado).getCodigo(); 
       Gremio objetoGremioAnadir = new Gremio(); 
       objetoGremioAnadir.setCodigo(codigoDelGremio); 
       objetoGremioAnadir.setLiteral(gremioQueQuiereAnadir); 
       gremios.add(objetoGremioAnadir); 
       adaptadorListaGremios.notifyDataSetChanged(); 

      } 
     }); 

回答

0
public class gremioAdapter extends ArrayAdapter<Gremio> { 

.... 

public void updateDataSet(Gremio objetoGremioAnadir) { 
    this.data.add(objetoGremioAnadir); 
    notifyDataSetChanged(); 

} 

} 

btnAnadirGremio.setOnClickListener(new OnClickListener() 
{ 

    @Override 
    public void onClick(View v) { 

     gremioQueQuiereAnadir = spinnerGremios.getSelectedItem().toString(); 
     codigoDelGremio = respuestaTerminar.getListaGremiosDisponibles().get(gremioClickeado).getCodigo(); 
     Gremio objetoGremioAnadir = new Gremio(); 
     objetoGremioAnadir.setCodigo(codigoDelGremio); 
     objetoGremioAnadir.setLiteral(gremioQueQuiereAnadir); 
     adaptadorListaGremios.updateDataSet(objetoGremioAnadir); 

     } 
}); 
+0

只添加第一次點擊,接下來不要,而不是去做,:( – colymore 2012-07-14 11:10:35

+0

使用onItemListClick, onClick監聽器,如果可以的話,最好給listview – Blackbelt 2012-07-14 11:12:39

+0

提供一些建議,儘量使用英文作爲變量和方法的名字 – Blackbelt 2012-07-14 11:13:33