Update Listview from a sorting Radiogroup

Asked

Viewed 41 times

2

I’m trying to update a ListView populated with database data from an ordination of a RadioGroup.

Here are my codes:

private List<ApplicationInfo> mAppList;
private SimpleCursorAdapter adptLivros;
private SwipeMenuListView lv_Livros;
private RadioGroup rgOrdenar;
private String orderBy;
private SQLiteDatabase db;

protected void onResume() {
    super.onResume();

    rgOrdenar = (RadioGroup)findViewById(R.id.rgOrdenar);
    orderBy = "";

    rgOrdenar.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()     {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.rbTitulo:
                        // trata radioValor1
                        orderBy = "no_titulo";
                        break;
                case R.id.rbAutor:
                    // trata radioValor2
                        orderBy = "no_autor";
                        break;
            }
        }
    });

    //estacia db_livroteca
    db = openOrCreateDatabase("db_livroteca",MODE_PRIVATE,null);

    //cria uma string para montar o sql
    StringBuilder sqlLivros = new StringBuilder();
    sqlLivros.append("SELECT * FROM tb_livros a ");
    sqlLivros.append("INNER JOIN tba_estado b ON ");
    sqlLivros.append(" b._id = id_estado");

    if(orderBy != ""){
        sqlLivros.append(" ORDER BY a."+orderBy+";");
    }else{
        sqlLivros.append(";");
    }

    //recupera os livros cadastrados
    Cursor csrLivros = db.rawQuery(sqlLivros.toString(),null);

    //estancia lvLivros
    lv_Livros = (SwipeMenuListView)findViewById(R.id.lvLivros);

    String[] fromLivros = {"_id","no_autor","no_titulo","no_edicao","no_editora","sg_estado","nu_ano_publicacao","nu_isbn"};
    int[] toLivros = {R.id.tvIdLivrosLV,R.id.tvAutorLV,R.id.tvTituloLV,R.id.tvEdicaoLV,R.id.tvEditoraLV,R.id.tvEstadoLV,R.id.tvAnoPublicacaoLV,R.id.tvISBN};
    adptLivros = new SimpleCursorAdapter(getBaseContext(),R.layout.model_lv_livros,csrLivros,fromLivros,toLivros);

    //seta os valores na lvLivros
    lv_Livros.setAdapter(adptLivros);

    db.close();
}

Who can help me thank you, I could not evolve from there.

Thank you

  • Do you understand that your if will always go into Else?

2 answers

0

Your problem is in the anonymous method you created to pass the orderBy. Whenever the onCheckedChanged is activated, it only assigns a value to its variable orderBy and does nothing else, I believe that by your example you should be thinking that he should leave the method and go through the other routines, but that’s not quite what happens.

You see, when you implement an anonymous method in any part of your code, it will only serve that part where it was implemented, it would be like you create a class, only the difference is that you are creating within your own method.

Do the following, move the whole part of the bank to a different method:

private void abreConsulta(String orderBy) {
    db = openOrCreateDatabase("db_livroteca",MODE_PRIVATE,null);

    //cria uma string para montar o sql
    StringBuilder sqlLivros = new StringBuilder();
    sqlLivros.append("SELECT * FROM tb_livros a ");
    sqlLivros.append("INNER JOIN tba_estado b ON ");
    sqlLivros.append(" b._id = id_estado");

    if(orderBy != ""){
        sqlLivros.append(" ORDER BY a."+orderBy+";");
    }else{
        sqlLivros.append(";");
    }

    //recupera os livros cadastrados
    Cursor csrLivros = db.rawQuery(sqlLivros.toString(),null);

    //estancia lvLivros
    lv_Livros = (SwipeMenuListView)findViewById(R.id.lvLivros);

    String[] fromLivros = {"_id","no_autor","no_titulo","no_edicao","no_editora","sg_estado","nu_ano_publicacao","nu_isbn"};
    int[] toLivros = {R.id.tvIdLivrosLV,R.id.tvAutorLV,R.id.tvTituloLV,R.id.tvEdicaoLV,R.id.tvEditoraLV,R.id.tvEstadoLV,R.id.tvAnoPublicacaoLV,R.id.tvISBN};
    adptLivros = new SimpleCursorAdapter(getBaseContext(),R.layout.model_lv_livros,csrLivros,fromLivros,toLivros);

    //seta os valores na lvLivros
    lv_Livros.setAdapter(adptLivros);

    db.close();
}

And in your event call the consultation again:

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rbTitulo:
                // trata radioValor1
                abreConsulta("no_titulo");
                break;
            case R.id.rbAutor:
                // trata radioValor2
                abreConsulta("no_autor");
                break;
        }
    }
  • Thanks for the help, but it does not redo the list, it even works to call the function abreConsulta and redo SQL, but Listview does not modify with the query. How would I update Listview?

  • Reformulating, just does not create in emulator, but tested on mobile and this working, thanks.

0

Some considerations:

1) You will always make the consultation in bank on onResume()?

2) I could use a list store strategy and sort it with Sort() - but then you have to implement a Customadapter/Custom Listview, it is much better than Simplecursoradapter, because you can customize it your way. That way, every time you make any list changes, sort, delete, include data, you can call notifyDataSetChanged();

@Override public void notifyDataSetChanged() { this.setNotifyOnChange(false);

this.sort(new Comparator<String>() {
    @Override
    public int compare(String lhs, String rhs) {
        return lhs.compareTo("orderBy");
    }
});

this.setNotifyOnChange(true);

}

3) Always compare strings using ". equals()"; In case your order by is empty and you want to check, use:

if(!orderby.equals("")){ . } Java string is an object, and because of the bytes, the comparison with equals is more "reliable".

  • I have learned to use onResume() whenever I have to update a list after I delete or change an item, to make the change without refreshing Activity. I did not put in the code but below what I wrote has the methods to change and delete in a Swipemenucreator.

  • I’ve been researching and the Customadapter is not natural Android right? I have to create it right?

  • And thanks for the tip of equals(), had already seen, but at the time I was programming I forgot this way to buy kkk.

  • Yes, the Customadapter is native, extending from Basedapter, there are several examples on the internet of it, opens a huge range of possibilities!

Browser other questions tagged

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