Concatenate TextView Fixed with String from Android Thread

0

I have a layout where I have a text set.

<TextView
            android:id="@+id/activityStockValorAnoTop_textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Maximo Anual:" />

My intention is that a value that I collect via web request from a thread of the activity, is concatenated with one another.

That is: If the fixed value is "Annual Maximum:" and the value that I have collected in the thread is "55.6", I will be attached to that TextView.

This is the code of the thread that makes the request, right now I get it in console with a System.out.println but. How do I attach that variable to the fixed value of the TextView?

    new Thread()
    {
        public void run()
        {

                while (true) {
                    try {
                    Thread.sleep(30000);
                        Stock stock = null;
                        try {
                            stock = YahooFinance.get("INTC");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        BigDecimal price = stock.getQuote().getPrice();

                        System.out.println(price);
                        System.out.println("----------------------------------");

                }
            catch(Exception ex)
                {
                }
            }
        }

    }.start();
    
asked by Eduardo 12.05.2017 в 10:50
source

1 answer

3

You should do it with an EventBus, which is going to launch an event and your subscribed activity will receive it, being able to do whatever you want with its value. The steps to follow:

In the module build.gradle add the following dependency:

compile 'org.greenrobot:eventbus:3.0.0'

Now we will have to make a POJO class, that is, an empty class with only the information you need, like this:

public class MessageEvent {

    public final String message;

    public MessageEvent(String message) {
        this.message = message;
    }

}

That variable 'message' will carry the value with it, so if it is not a String you should change the data type.

Now you have to declare in the activity these 2 methods, which will subscribe to the event and will "unsubscribe" (so that we understand each other ..)

EYE: If you already have the onStart method and the onStop in your activity, it obviously copies only the inside.

@Override
public void onStart() {
    super.onStart();
    // Se registra en el bus por defecto.
    EventBus.getDefault().register(this);
}

@Override
public void onStop() {
    // Se desregistra del bus por defecto.
    EventBus.getDefault().unregister(this);
    super.onStop();
}

Once again in the activity, you will have to copy the following, which is neither more nor less than what you want to do when the event 'skips':

I suppose that you have already obtained in the onCreate method the FindViewById of the element, in this way (and declaring the variable 'text' at the class level):

texto = (TextView) findViewById(R.id.activityStockValorAnoTop_textView);

so the result would be the same:

@Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
    texto.setText("Maximo Anual: "+ event.message);
}

Finally, you only have to send the event and publish it in the secondary thread, with this simple sentence:

EventBus.getDefault().post(new MessageEvent(variable_resultado));

Obviously replace 'result variable' with the value of the result obtained in your case.

    
answered by 12.05.2017 / 11:27
source