我有这段代码,问题是我滚动时重复元素。我解决了这个问题,从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);
}
我觉得你adpter问题只是尝试与adpter :: CustomAdapter adpter =新CustomAdapterthis)添加以下代码; adpter.notifyDataSetChanged(); listView.setAdapter(adpter); listView.invalidate(); – CoronaPintu
没有做任何事情:( – mesacuadrada