Infinite listview on android

2

I have an app that consumes a web service, but I have a generic call that searches all users and this is taking too long because I have several records, I want to do so the user goes down the screen and there is more data in the listview , but my call in the web service is like SELECT * FROM ALUNOS , and I would like to make a limit, my question is: will I have to make multiple limits or just implementing OnScrollListener is it already right?

I'm lost, I do not know how to implement this.

My method of searchingAll of the WEB SERVICES Student

public ArrayList<Aluno> buscarTodosUsuarios() {

    ArrayList<Aluno> lista = new ArrayList<Aluno>();

    try {

        Connection conn = ConectaMySql.obtemConexao();

        String queryInserir = "SELECT * FROM ALUNO ORDER BY NOME";

        PreparedStatement ppStm = conn.prepareStatement(queryInserir);

        ResultSet rSet = ppStm.executeQuery();

        while (rSet.next()) {

            Aluno user = new Aluno();

            user.setId(rSet.getInt(1));

            user.setNome(rSet.getString(2));

            user.setLogin(rSet.getString(3));

            user.setPass(rSet.getString(4));

            user.setCurso(rSet.getString(5));

            user.setSegunda(rSet.getString(6));

            user.setM1(rSet.getString(7));

            user.setTerca(rSet.getString(8));

            user.setM2(rSet.getString(9));

            user.setQuarta(rSet.getString(10));

            user.setM3(rSet.getString(11));

            user.setQuinta(rSet.getString(12));

            user.setM4(rSet.getString(13));

            user.setSexta(rSet.getString(14));

            user.setM5(rSet.getString(15));

            user.setFoto(rSet.getBytes(16));

            lista.add(user);

        }

        conn.close();

    } catch (Exception e) {

        e.printStackTrace();

        return null;

    }

    return lista;

}

My Student's DAO method of my application

public List<Aluno> buscarTodosUsuarios() {

    List<Aluno> listaUsr = new ArrayList<Aluno>();

    SoapObject buscarUsuarios = new SoapObject(NAMESPACE, BUSCAR_TODOS);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);

    envelope.setOutputSoapObject(buscarUsuarios);

    envelope.implicitTypes = true;

    HttpTransportSE http = new HttpTransportSE(URL);

    try {
        http.call("urn:" + BUSCAR_TODOS, envelope);

        if (envelope.getResponse() instanceof SoapObject) {
            SoapObject resposta = (SoapObject) envelope.getResponse();

            Aluno usr = new Aluno();

            usr.setId(Integer.parseInt(resposta.getProperty("id")
                    .toString()));
            usr.setNome(resposta.getProperty("nome").toString());
            usr.setCurso(resposta.getProperty("curso").toString());
            usr.setSegunda(resposta.getProperty("segunda").toString());
            usr.setM1(resposta.getProperty("m1").toString());
            usr.setTerca(resposta.getProperty("terca").toString());
            usr.setM2(resposta.getProperty("m2").toString());
            usr.setQuarta(resposta.getProperty("quarta").toString());
            usr.setM3(resposta.getProperty("m3").toString());
            usr.setQuinta(resposta.getProperty("quinta").toString());
            usr.setM4(resposta.getProperty("m4").toString());
            usr.setSexta(resposta.getProperty("sexta").toString());
            usr.setM5(resposta.getProperty("m5").toString());
            String foto = resposta.getProperty("foto").toString();

            usr.setTesteFoto(foto.toString());

            byte[] bt = Base64.decode(foto, Base64.DEFAULT);
            usr.setFoto(bt);
            listaUsr.add(usr);
        } else {
            Vector<SoapObject> retorno = (Vector<SoapObject>) envelope
                    .getResponse();

            for (SoapObject resposta : retorno) {

                Aluno usr = new Aluno();

                usr.setId(Integer.parseInt(resposta.getProperty("id")
                        .toString()));
                usr.setNome(resposta.getProperty("nome").toString());
                usr.setCurso(resposta.getProperty("curso").toString());
                usr.setSegunda(resposta.getProperty("segunda").toString());
                usr.setM1(resposta.getProperty("m1").toString());
                usr.setTerca(resposta.getProperty("terca").toString());
                usr.setM2(resposta.getProperty("m2").toString());
                usr.setQuarta(resposta.getProperty("quarta").toString());
                usr.setM3(resposta.getProperty("m3").toString());
                usr.setQuinta(resposta.getProperty("quinta").toString());
                usr.setM4(resposta.getProperty("m4").toString());
                usr.setSexta(resposta.getProperty("sexta").toString());
                usr.setM5(resposta.getProperty("m5").toString());
                String foto = resposta.getProperty("foto").toString();

                usr.setTesteFoto(foto.toString());

                byte[] bt = Base64.decode(foto, Base64.DEFAULT);
                usr.setFoto(bt);
                listaUsr.add(usr);
            }
        }

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

    return listaUsr;
}

The class that gets my listview

public class ListarTodosActivity extends Activity {

private ListView lvUsuario;
private List<Aluno> usrs;
private AlunoDAO dao;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listartodos);

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);

    dao = new AlunoDAO();
    lvUsuario = (ListView) findViewById(R.id.lvListarTodos);

    usrs = dao.buscarTodosUsuarios();

    AlunoAdapter usrAdp = new AlunoAdapter(this, usrs);
    lvUsuario.setAdapter(usrAdp);

    lvUsuario.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Aluno aluno = (Aluno) lvUsuario.getItemAtPosition(position);

            Intent intent = new Intent(ListarTodosActivity.this,
                    DetalhesActivity.class);
            intent.putExtra("ALUNO_NOME", aluno.getNome().toString());
            intent.putExtra("ALUNO_CURSO", aluno.getCurso().toString());
            intent.putExtra("ALUNO_FOTO", aluno.getTesteFoto().toString());// envio
                                                                            // a
                                                                            // string
                                                                            // da
                                                                            // foto
                                                                            // do
                                                                            // banco
                                                                            // para
                                                                            // meu
                                                                            // detalhesActivity.class
            intent.putExtra("ALUNO_DIA1", aluno.getSegunda().toString());
            intent.putExtra("ALUNO_M1", aluno.getM1().toString());
            intent.putExtra("ALUNO_DIA2", aluno.getTerca().toString());
            intent.putExtra("ALUNO_M2", aluno.getM2().toString());
            intent.putExtra("ALUNO_DIA3", aluno.getQuarta().toString());
            intent.putExtra("ALUNO_M3", aluno.getM3().toString());
            intent.putExtra("ALUNO_DIA4", aluno.getQuinta().toString());
            intent.putExtra("ALUNO_M4", aluno.getM4().toString());
            intent.putExtra("ALUNO_DIA5", aluno.getSexta().toString());
            intent.putExtra("ALUNO_M5", aluno.getM5().toString());

            startActivity(intent);

        }

    });
}

My Adapter class from my listview

public class AlunoAdapter extends BaseAdapter {

private List<Aluno> usrs;
private Context context;

public AlunoAdapter(Context context, List<Aluno> usrs) {
    this.usrs = usrs;
    this.context = context;
}

@Override
public int getCount() {
    return usrs.size();
}

@Override
public Object getItem(int arg0) {
    return usrs.get(arg0);
}

@Override
public long getItemId(int arg0) {
    return usrs.get(arg0).getId();
}

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

    View rootView = LayoutInflater.from(context).inflate(
            R.layout.activity_modelo_aluno, parent, false);

    ImageView imgFoto = (ImageView) rootView.findViewById(R.id.imgFoto);
    TextView txtNome = (TextView) rootView.findViewById(R.id.txtNome);
    TextView txtCurso = (TextView) rootView.findViewById(R.id.txtCurso);

    Aluno usuarioDaVez = usrs.get(position);

    txtNome.setText(usuarioDaVez.getNome());
    txtCurso.setText(usuarioDaVez.getCurso());

     Bitmap bitmap = BitmapFactory.decodeByteArray(usuarioDaVez.getFoto(),
     0, usuarioDaVez.getFoto().length);
     imgFoto.setImageBitmap(bitmap);

    return rootView;
}}
    
asked by anonymous 31.07.2015 / 00:55

2 answers

3

The main problem is that you are making a query and returning a list of Students. Ideally you would use CursorLoader and CursorAdapter . The main advantages of using CursorLoader are:

  • Data loading is done in the background (outside the UIThread), so the screen does not crash when loading data.
  • Data loading is done on demand, that is, if your screen can only display 10 items, only 10 items will be loaded into memory and as you slide the list the other items will be loaded into memory. >
  • 31.07.2015 / 05:43
    2

    You will need to adapt the SQL's of your web service to work with LIMIT and OFFSET . This way you will be able to list users within a range [0-10].

    Already in the application, there are several libraries that make this management to make the request and update the ListView or RecyclerView . If you can not find it, you can find out if the last View of your list is being displayed by the mListView.getLastVisiblePosition() method.

    I hope it helps!

        
    31.07.2015 / 04:32