Update ListView from a SortGroupGroup

2

I'm trying to update a ListView populated with database data from a sort of 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();
}

Anyone can help me thanks, I could not evolve from there.

Thank you

    
asked by anonymous 08.07.2016 / 06:01

2 answers

0

Your problem is in the anonymous method you created to pass orderBy . Whenever onCheckedChanged is enabled, it just assigns a value to its orderBy variable and does nothing else, I think by its example you must be thinking that it should exit the method and pass in the other routines, but it is not Well that happens.

You see, when you implement an anonymous method in any part of your code, it will only serve the part where it was implemented, it would be as if you created a class, but the difference is that you are creating it inside 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 query 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;
        }
    }
    
08.07.2016 / 13:44
0

Some considerations:

1) Will you always check in onResume ()?

2) I could use a strategy of storing in a list and sorting it with sort () - however, there you will 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 changes to the list, sorting, deleting, adding 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 the strings using the ".equals ()";    In case your order by being empty and you want to check, use:

if (! orderBy.equals ("")) { .. } String in java is an object, and by the count of the bytes, the comparison with equals is more "reliable."

    
08.07.2016 / 13:51