0

我有这段代码,问题是我滚动时得到了重复的元素。我解决了这个从 if(item==null) 中取出部分代码的问题,但是我会做很多数据库调用而不是我需要的 6 个(对于六个元素)

如您所见,留下代码,我只有 6 个 DB 调用,但也有重复的项目...我在这里和那里阅读了很多关于此的内容,但我不明白这是如何工作的...

我还阅读了一些关于布局中清晰元素的内容,但根本不明白......你能帮我吗?

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);

        }
4

3 回答 3

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;
}
于 2013-06-26T08:30:05.083 回答
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));
于 2013-06-26T08:23:44.920 回答
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
于 2013-06-26T08:37:47.473 回答