2013-06-26 61 views
0

我有這段代碼,問題是我滾動時重複元素。我解決了這個問題,從if(item == null)中獲取部分代碼,但隨後我會做很多數據庫調用,而不是我需要的6個(對於六個元素)ListView重複的項目滾動時和數據庫

離開代碼看,我只有6個DB電話,但也重複項目...我讀了很多關於這個在這裏和那裏,但我不明白這是如何工作的...

我也讀somethig有關清晰的元素在佈局中,但根本不理解...... 請問你能幫我嗎?

static class ViewHolder { 
    TextView totales; 
    TextView falladas; 
    TextView nota; 
    CheckedTextView checkList; 
} 

public class AdaptadorTemas extends ArrayAdapter<String> { 

    Activity context; 

    public AdaptadorTemas(Activity context, ArrayList<String> values) { 
     super(context, R.layout.elementos_lista_url, values); 
     this.context = context; 
     this.values = values; 
    } 

    @Override 
    public View getView(int position, View item, ViewGroup parent) { // es llamado cada vez que se muetras un elemento de la lista 


     if(item == null){ 

      LayoutInflater inflater = context.getLayoutInflater(); 
      item = inflater.inflate(R.layout.elementos_lista_temas, null); 

      //here I do some sql querys to fill the holder variables 
      //... 


      holder = new ViewHolder(); 
      holder.totales = (TextView)item.findViewById(R.id.lblPreguntas); 
      holder.falladas = (TextView)item.findViewById(R.id.lblFalladas); 
      holder.nota = (TextView)item.findViewById(R.id.lblNota); 
      holder.checkList = (CheckedTextView)item.findViewById(R.id.checkList); 

      item.setTag(holder); 

      //then I fill the variables 
      holder.falladas.setText(strFalladas); 
      holder.nota.setText(nota); 
      holder.totales.setText(totalRespondidas + "/" + totales + " - " + porcentaje + "%"); 
      holder.checkList.setText(values.get(position)); 

     } 
     else{ 
      holder = (ViewHolder)item.getTag(); 
     } 

     return item; 


    } // getView 


} // class 

謝謝你們你的答案,但最終我存儲的每個元素在的ArrayList(與數據),並在每個getView要求ArrayList中該元素,硬一點點的瞭解如何ListView控件與持有人對我的作品...... 下面的代碼:

@Override 
    public View getView(int position, View item, ViewGroup parent) { // es llamado cada vez que se muetras un elemento de la lista 

     ViewHolder holder = null; 
     final String elemento = values.get(position); 
     int totalRespondidas = 0; 
     int falladas = 0; 
     int totales = 0; 
     int porcentaje = 0; 
     String nota = "-"; 
     String strFalladas = ""; 

     if(item == null){ 
      LayoutInflater inflater = context.getLayoutInflater(); 
      item = inflater.inflate(R.layout.elementos_lista_temas, null); 

      holder = new ViewHolder(); 
      holder.totales = (TextView)item.findViewById(R.id.lblPreguntas); 
      holder.falladas = (TextView)item.findViewById(R.id.lblFalladas); 
      holder.nota = (TextView)item.findViewById(R.id.lblNota); 
      holder.checkList = (CheckedTextView)item.findViewById(R.id.checkList); 

      item.setTag(holder); 

     } 
     else{ 
      holder = (ViewHolder)item.getTag(); 
     } 

     boolean loTenemos = false; 
     int i = 0; 

     for(i=0; i<elementos.size(); i++){ 
      String[] dividido = elementos.get(i).split("#"); 

      if(dividido[0].equals(elemento)){ 
       //System.out.println(elementos.get(i) + " coincide con " + elemento); 
       loTenemos = true; 
       //System.out.println("encontrado en " + i + " de " + elementos.size()); 
       break; 
      } 

     } 

     if(loTenemos){ 
      //here i get the data from the vector 

      vCounter ++ ; 
      System.out.println("vector " + vCounter); 

      String[] datos = elementos.get(i).split("#"); 
      falladas = Integer.valueOf(datos[1]); 
      nota = datos[2]; 
      totalRespondidas = Integer.valueOf(datos[3]); 
      totales = Integer.valueOf(datos[4]); 

      holder.falladas.setText(strFalladas); 
      holder.nota.setText(nota); 
      holder.totales.setText(totalRespondidas + "/" + totales + " - " + porcentaje + "%"); 
      holder.checkList.setText(values.get(position)); 

     } 
     else{ 

      //here I get the data from the DB 
      //and stored it in the Vector 
      elementos.add(elemento + "#" + falladas + "#" + nota + "#" + totalRespondidas + "#" + totales); 

     } 
+0

我覺得你adpter問題只是嘗試與adpter :: CustomAdapter adpter =新CustomAdapterthis)添加以下代碼; adpter.notifyDataSetChanged(); listView.setAdapter(adpter); listView.invalidate(); – CoronaPintu

+0

沒有做任何事情:( – mesacuadrada

回答

1

你應該充滿變量值超出條件身邊...

if(item == null){ 

     LayoutInflater inflater = context.getLayoutInflater(); 
     item = inflater.inflate(R.layout.elementos_lista_temas, null); 

     //here I do some sql querys to fill the holder variables 
     //... 


     holder = new ViewHolder(); 
     holder.totales = (TextView)item.findViewById(R.id.lblPreguntas); 
     holder.falladas = (TextView)item.findViewById(R.id.lblFalladas); 
     holder.nota = (TextView)item.findViewById(R.id.lblNota); 
     holder.checkList = (CheckedTextView)item.findViewById(R.id.checkList); 

     item.setTag(holder); 

     //then I fill the variables 

    } 
    else{ 
     holder = (ViewHolder)item.getTag(); 
    } 
     holder.falladas.setText(strFalladas); 
     holder.nota.setText(nota); 
     holder.totales.setText(totalRespondidas + "/" + totales + " - " + porcentaje + "%"); 
     holder.checkList.setText(values.get(position)); 
2

嘗試以下

使你的ViewHolder和你的類的內部類AdaptadorTemas

static class ViewHolder { 
TextView totales; 
TextView falladas; 
TextView nota; 
CheckedTextView checkList; 
} 

@Override 
public View getView(int position, View item, ViewGroup parent) { 
    ViewHolder holder; 
    if(item == null){ 

     LayoutInflater inflater = context.getLayoutInflater(); 
     item = inflater.inflate(R.layout.elementos_lista_temas, null); 
     holder = new ViewHolder(); 
     holder.totales = (TextView)item.findViewById(R.id.lblPreguntas); 
     holder.falladas = (TextView)item.findViewById(R.id.lblFalladas); 
     holder.nota = (TextView)item.findViewById(R.id.lblNota); 
     holder.checkList = (CheckedTextView)item.findViewById(R.id.checkList); 

     item.setTag(holder); 
     } 
    else{ 
     holder = (ViewHolder)item.getTag(); 
    } 
     holder.falladas.setText(strFalladas); 
     holder.nota.setText(nota); 
     holder.totales.setText(totalRespondidas + "/" + totales + " - " + porcentaje + "%"); 
     holder.checkList.setText(values.get(position)); 

    return item; 
} 
0

你必須改變你的getView方法,因爲它有時會清除視圖,然後才能再次創建視圖不是他們的價值觀,因此,你需要重新填充數據。或者你可以以某種方式保存sql查詢的數據,如內存或sqlite等,如果已經存在,你可以在那裏閱讀。

@Override 
public View getView(int position, View item, ViewGroup parent) { 

     if (item == null) { 
      LayoutInflater inflater = context.getLayoutInflater(); 
      item = inflater.inflate(R.layout.elementos_lista_temas, null); 
     } 

     //check data if its in sqlite if not query your database and then save it in sqlite or static list etc whatever you choose 
     strFalladas = getDataFromSomewhere(); 
     ... 
     ... 

     holder = new ViewHolder(); 
     holder.totales = (TextView)item.findViewById(R.id.lblPreguntas); 
     holder.falladas = (TextView)item.findViewById(R.id.lblFalladas); 
     holder.nota = (TextView)item.findViewById(R.id.lblNota); 
     holder.checkList = (CheckedTextView)item.findViewById(R.id.checkList); 

     item.setTag(holder); 

     //then I fill the variables 
     holder.falladas.setText(strFalladas); 
     holder.nota.setText(nota); 
     holder.totales.setText(totalRespondidas + "/" + totales + " - " + porcentaje + "%"); 
     holder.checkList.setText(values.get(position)); 

    return item; 


} // getView 
+0

然後,爲什麼.setTag()? – mesacuadrada