Android - Scratch text on a Listview

Asked

Viewed 418 times

1

I created an application of tasks to be done that are presented in a Listview.

I am using Sqlite with a table with the columns: ID, task, completed.

I want that when I go through the records, if the "completed" column equals "s" the text in Listview appears with a scratch in the middle of it. Is there any way to do that?

This code is in the retrieveTarefas() method that searches for all tasks registered in the Sqlite database:

//Recuperar as tarefas
        Cursor cursor = bancoDeDados.rawQuery("SELECT * FROM tarefas ORDER BY id DESC", null);

        //recuperar ids das colunas
        int indiceColunaId = cursor.getColumnIndex("id");
        int indiceColunaTarefa = cursor.getColumnIndex("tarefa");
        int indiceColunaConcluida = cursor.getColumnIndex("concluida");


        //cria o adaptador
        itens = new ArrayList<String>();
        itensAdaptador = new ArrayAdapter<String>(getApplicationContext(),
                R.layout.items_list,
                android.R.id.text1,
                itens);

        idsTarefas = new ArrayList<Integer>();
        listaTarefas.setAdapter(itensAdaptador);

        //Lista as tarefas - quando usa o rawquery ele fica parado no ultimo registro
        cursor.moveToFirst();
        while (cursor != null){

            if (cursor.getString(indiceColunaConcluida) == "s"){

                itens.add(cursor.getString(indiceColunaTarefa));
                //Aqui quero colocar que o texto fica riscado

            } else {
                itens.add(cursor.getString( indiceColunaTarefa ));
                //Aqui o texto deve ficar normal (sem risco)
            }

            idsTarefas.add( Integer.parseInt( cursor.getString(indiceColunaId) ) );
            cursor.moveToNext();

        }

2 answers

5

I hope this helps.

TextView tv = (TextView) findViewById(R.id.mytext);
    tv.setText("Texto com risco");
    tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
  • In a textview it works. But how to do it in a Listview? That I can’t apply

2


What I did to scratch the text on Listview was the following:

Instead of creating String type Arraylist I switched to Spannablearraylist:

private ArrayAdapter<SpannableString> itensAdaptador;
private ArrayList<SpannableString> itens;

And I used the Spannablestring class to create a crossed-out text as follows:

 SpannableString textoRiscado = new SpannableString(cursor.getString(indiceColunaTarefa));
 textoRiscado.setSpan(new StrikethroughSpan(), 0, textoRiscado.length(), 0 );

And then I simply added the text object.

itens.add(textoRiscado);

Worked perfectly

Browser other questions tagged

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