Skip to content Skip to sidebar Skip to footer

Get A String Value From A Thread

can someone tell me, how do get a Stringvalue from a thread to the mainActivity? i have a thread like this: public class XMLHandler extends DefaultHandler { XMLDataCollected data

Solution 1:

If you have a SeparateThread class then you need to create one Interface say

public interface FetchValueListener{

public void sendValue(String value_to_send);

}

And your acctivity will be implementing this interface and thus sendValue(value_to_send) method will be added to your activity.

Next step would be when you create the object of the THread class then you need to pass the object of that interface in the paramater as follows:

    public class myThreadClass{
    FetchValueListener mllistener;

    myThreadClass(FetchValueListener listenerObj){
         mllistener=listenerObj;
    }

    }

Now when you want to send some value to the activity from thread you can just simply call

mllistener.sendValue(value_you_wan_to_send);

And inside your actiivty you will get the value in the sendValue() method..

In that method you need to post the data to runnable using the handler so that you can make changes to the UI like setText etc..... If you directly try to set the value of text view in that method you will get an exception.


Post a Comment for "Get A String Value From A Thread"