I need to separate a String message in 6 Textviews

Asked

Viewed 32 times

1

I’m making an app with bluetooth connection and it captures the messages or data that are sent by the Internet via Bluetooth, I’m sending these messages via Bluetooth, each call sends a data and each data should be shown in your specific Textview, then how do I direct this data to your specific Textview? The exchange of information already happens only that the only data is sent and appears in all Textview does not appear in only one, and I would like it to be separate information for example temperature in one and humidity in another.

   /*
    * Função que envia a mensagem do celular para o módulo Bluetooth, exibindo no monitor serial.
    */
    private void enviar(String str) {
        if(connected != Connected.True) {
            Toast.makeText(getActivity(), "Não conectado", Toast.LENGTH_SHORT).show();
            return;
        }
        try {
            String msg;
            byte[] data;
                msg = str;
                data = (str + newline).getBytes();
            SpannableStringBuilder spn = new SpannableStringBuilder(msg+'\n');
            spn.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorSendText)), 0, spn.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            receiveText.append(spn);
            service.write(data);
        } catch (Exception e) {
            onSerialIoError(e);
        }
    }

    //Envia a mensagem para o receptor, ou seja, o celular... do arduino para o celular
    private void receptor(byte[] data) {
            String msg = new String(data);

            /*
            if(newline.equals(newline_crlf) && msg.length() > 0) {
                // não mostra CRLF como ^M se diretamente antes de LF
                msg = msg.replace(newline_crlf, newline_lf);
                // tratamento especial se CRLF e LF vierem em fragmentos separados
                if (pendingNewline && msg.charAt(0) == '\n') {
                    Editable edt = receiveText.getEditableText();
                    if (edt != null && edt.length() > 1)
                        edt.replace(edt.length() - 2, edt.length(), "");
                }
                pendingNewline = msg.charAt(msg.length() - 1) == '\r';
            }*/

        humidadeSolo.append(TextUtil.toCaretString(msg, newline.length() != 0));
        receiveText.append(TextUtil.toCaretString(msg, newline.length() != 0));
        
        }

This other Snippet of code treats the information received to be displayed in Textview, plus which treatment I should employ to fragment that message into two parts or more and I target it to your respective Textviews, please help me...

 static CharSequence toCaretString(CharSequence s, boolean keepNewline) {
        return toCaretString(s, keepNewline, s.length());
    }

    static CharSequence toCaretString(CharSequence s, boolean keepNewline, int length) {
        boolean found = false;
        for (int pos = 0; pos < length; pos++) {
            if (s.charAt(pos) < 32 && (!keepNewline ||s.charAt(pos)!='\n')) {
                found = true;
                break;
            }
        }
        if(!found)
            return s;
        SpannableStringBuilder sb = new SpannableStringBuilder();
        for(int pos=0; pos<length; pos++)
            if (s.charAt(pos) < 32 && (!keepNewline ||s.charAt(pos)!='\n')) {
                sb.append('^');
                sb.append((char)(s.charAt(pos) + 64));
                sb.setSpan(new BackgroundColorSpan(caretBackground), sb.length()-2, sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else {
                sb.append(s.charAt(pos));
            }
        return sb;
    }

2 answers

1

Tip: See the documentation about Textviews

To define specific texts for different Textviews You first need to define the id (@+id) of each one, then use the desired method pointing to the correct id.

  • Ready I’m trying to do this, but my doubt even is as I will separate this String without giving errors?

0

I was able to solve the problem using:

/*
 *Abordagem utilizando split.
 */
String[] speed = dataString.split("@");
temperatura.setText("");
temperatura.append(speed[0]);
umidadeAr.setText("");
umidadeAr.append(speed[1]);
altitude.setText("");
altitude.append(speed[2]);
pressao.setText("");
pressao.append(speed[3]);
velVento.setText("");
velVento.append(speed[4]);
chuva.setText("");
chuva.append(speed[5]);
umiSolo.setText("");
umiSolo.append(speed[6]);
qualiAr.setText("");
qualiAr.append(speed[7]);

/* *Split approach. */ String[] speed = dataString.split("@"); setText("") temperature; append(speed[0]) temperature; humidityAr.setText(""); humidityAr.append(speed[1]); setText("") altitude; append altitude(speed[2]); pressure.setText(""); pressao.append(speed[3]); Velvento.setText(""); Velvento.append(speed[4]); setText rain(""); rain.append(speed[5]); umiSolo.setText(""); umiSolo.append(speed[6]); qualiAr.setText(""); qualiAr.append(speed[7]);

Browser other questions tagged

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