滚动和DB时ListView重复的项目

时间:2013-06-26 08:18:02

标签: android listview

我有这段代码,问题是当我滚动时我得到了重复的元素。我解决了从if(item == null)中取出部分代码但后来我会做很多DB调用而不是我需要的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);

        }

3 个答案:

答案 0 :(得分:2)

尝试以下

制作班级ViewHolder的{​​{1}}和内部班级。

AdaptadorTemas

答案 1 :(得分: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 :(得分: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