Listview infinity on android

Asked

Viewed 198 times

2

I have an app that consumes a web service, only I have a generic call that searches all users and this taking a long time because I have several records, I want to do so the user goes down the screen and appears more data in listview, only that my web service call is like this SELECT * FROM ALUNOS, and I would like to make a limit, my question is: will I have to do multiple Limits or just implementing the OnScrollListener he’s already sure?

I’m lost I don’t know how to implement this.

My WEB SERVICE Alunodal Method

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 Alunodao 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 catches 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 listview Adapter class

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

2 answers

3

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

  1. Data loading is done in the background (outside of Uithread), so the screen does not lock while loading the data.
  2. Data loading is done on demand, that is, if your screen can only show 10 items, only 10 items will be loaded in memory and as you slide the list the other items will be loaded in memory.

2

You will need to adapt the SQL’s of your web service to work with LIMITand OFFSET. This way you can list users within a range [0-10].

Already in the application, there are several libraries that do this management to perform the request and update the ListView or RecyclerView. If you don’t find it, you can know if the last View of your list is being displayed through the method mListView.getLastVisiblePosition().

I hope it helps!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.