Listview在Android上使用无限滚动重复项目

时间:2014-09-17 01:17:37

标签: android listview android-listview infinite-scroll

我构建了一个listview并实现了一个无限滚动,listview限制为每个加载显示5个项目,直到它到达结尾,但它复制了最初的5个项目,我将添加一些图像,以便您可以更好地理解:

enter image description here

我该如何解决?

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {


        // TODO Auto-generated method stub
        rootView = inflater.inflate(R.layout._fragment_clientes, container, false);

        rootView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT ));


        try {

            lv = (ListView) rootView.findViewById(R.id.listaClientes);

            clientes = new ArrayList<ClienteModel>();
            final ClientViewAdapter ad = new ClientViewAdapter(getActivity(), this, clientes);

            lv.setVerticalFadingEdgeEnabled(true);
            lv.setVerticalScrollBarEnabled(true);

            lv.setOnScrollListener(new EndlessScrollListener(){
                @Override
                public void onLoadMore(int page, int totalItemsCount) {
                    new LoadMoreClientTask(progressBar,FragmentClientes.this,ad,getActivity()).execute(page);
                }
            });

            lv.addFooterView(footerLinearLayout);


            lv.setAdapter(ad);
            new LoadMoreClientTask(progressBar,this,ad,getActivity()).execute(1);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return rootView;
    }

适配器:

public class ClientViewAdapter extends BaseAdapter {

    private Activity activity;
    private FragmentClientes frag;
    private List<ClienteModel> cliente;
    private static LayoutInflater inflater=null;

    public ClientViewAdapter(Context context, FragmentClientes fragmentClientes, List<ClienteModel> clientes) {
        this.inflater = LayoutInflater.from( context );
        this.cliente = clientes;

        frag = fragmentClientes;

    }

    public int getCount() {

        return cliente.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        View vi=convertView;
        ViewHolder holder;
        if(convertView == null){
            vi = inflater.inflate(R.layout.fragment_cliente_item, null);
            holder=new ViewHolder();
            holder.id = (TextView)vi.findViewById(R.id.clienteId);
            holder.nome = (TextView)vi.findViewById(R.id.clienteNome);
            holder.tipo = (TextView)vi.findViewById(R.id.clienteTipo);

            vi.setTag(holder);
        }else{
            holder = (ViewHolder)vi.getTag();
        }

        ClienteModel item = new ClienteModel();
        item = cliente.get(position);

        holder.id.setText(String.valueOf(item.getClientes_id()));
        holder.nome.setText(item.getNome());
        holder.tipo.setText(item.getTipo());

        return vi;
    }

    public void setData(List<ClienteModel> clientes){
        this.cliente.addAll(clientes);
        this.notifyDataSetChanged();
    }

    public class ViewHolder
    {
        TextView id;
        TextView nome;
        TextView tipo;

    }
}

从数据库获取数据的LoadMoreTask片段:

  protected Boolean doInBackground(Integer... parameters) {
        int npagina = parameters[0];
        cliente= new ArrayList<ClienteModel>();

        try {

            Repositorio mRepositorio = new Repositorio(context);


            List listaDeClientes = mRepositorio.getClientes(npagina,5,"");

            cliente = listaDeClientes;

            System.out.println("pagina " + npagina);

        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

函数getClientes:

 public List<ClienteModel> getClientes(Integer pagina, Integer limit, String consulta) throws SQLException {

        Integer offset = pagina * limit - limit;


        List<ClienteModel> listaDeRegistros = new ArrayList<ClienteModel>();



        if(consulta.isEmpty()) {
            query = "SELECT * FROM " + tabelaCLIENTES + " WHERE credencial_id = " + mSessao.getString("id_credencial") + " LIMIT " + offset + ", " + limit;
        }else {
            query = "SELECT * FROM " + tabelaCLIENTES + " WHERE (credencial_id = " + mSessao.getString("id_credencial") + ") and (nome LIKE '%"+consulta+"%') LIMIT " + offset + ", " + limit;
        }

        System.out.println(query);

        try {

            Cursor mCursor = bd.rawQuery(query, null);

            if (mCursor.getCount() > 0) {
                if (mCursor.moveToFirst()) {
                    do {
                        ClienteModel mClienteModel = new ClienteModel();

                        mClienteModel.setClientes_id(mCursor.getInt(mCursor.getColumnIndex(ClienteModel.Coluna.CLIENTES_ID)));
                        mClienteModel.setId_rm(mCursor.getInt(mCursor.getColumnIndex(ClienteModel.Coluna.ID_RM)));
                        mClienteModel.setCredencial_id(mCursor.getInt(mCursor.getColumnIndex(ClienteModel.Coluna.CREDENCIAL_ID)));
                        mClienteModel.setNome(mCursor.getString(mCursor.getColumnIndex(ClienteModel.Coluna.NOME)));
                        mClienteModel.setTipo(mCursor.getString(mCursor.getColumnIndex(ClienteModel.Coluna.TIPO)));
                        mClienteModel.setInformacao_adicional(mCursor.getString(mCursor.getColumnIndex(ClienteModel.Coluna.INFORMACAO_ADICIONAL)));
                        mClienteModel.set_criado(mCursor.getString(mCursor.getColumnIndex(ClienteModel.Coluna._CRIADO)));
                        mClienteModel.set_modificado(mCursor.getString(mCursor.getColumnIndex(ClienteModel.Coluna._MODIFICADO)));
                        mClienteModel.set_status(mCursor.getString(mCursor.getColumnIndex(ClienteModel.Coluna._STATUS)));

                        listaDeRegistros.add(mClienteModel);

                    } while (mCursor.moveToNext());
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return listaDeRegistros;
    }

2 个答案:

答案 0 :(得分:1)

当您要结束加​​载更多时,您的加载代码只是重新加载相同的5个条目。您需要检查已加载的内容并验证是否结束,以便停止添加条目。

答案 1 :(得分:0)

尝试这个(带偏移的交换限制):

query = "SELECT * FROM " + tabelaCLIENTES + " WHERE credencial_id = " + mSessao.getString("id_credencial") + " LIMIT " + limit + ", " + offset;