Search code examples
javaandroidandroid-fragmentshandlerioio

Passing data to a handler, from another class


I have a Handler that is in my MainActivity. This Handler has to update the UI of one of my Fragments inside of the MainActivity's layout.

I then have a separate class that has a loop; when I create this class, let's called it myLooper it does some sort of calculation then send the information inside of the loop to the Handler in my MainActivity. Which in turn will then update that data onto a GraphView that I have implemented within my MainActivity's Fragment.

FLOW: Step 1: Calculate data on MyLooper. Step 2: Send data to MainActivity via my Handler. Step 3: Have my Handler update MainActivity's Fragment's GraphView with the sent data.

Relevant code:

public class MainActivity extends ActionBarActivity implements
        ActionBar.TabListener {


    public MyLooper myDataLooper;
    Fragment graphFrag = fragment_graphpage.newInstance(1);
    public final Handler myHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub

                ((fragment_graphpage) graphFrag).updateGraphUI(msg.getData().getFloat("myvoltage"));
            }
        };
    SectionsPagerAdapter mSectionsPagerAdapter;
    globalControlInstance globalControl;
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        globalControl = globalControlInstance.getInstance();

        myDataLooper = (IOIOBoard) getApplicationContext();
        myDataLooper.create(getBaseContext(), this,_myIOIOHandler);
        myDataLooper.start();
 }

MyLooper class:

public class MyLooper extends Application implements IOIOLooperProvider {


    globalControlInstance globalData = globalControlInstance.getInstance();
    public AnalogInput AI1;
    public Handler IOIOHandler;
    private final IOIOAndroidApplicationHelper helper_ = new IOIOAndroidApplicationHelper(
            this, this);

    protected void create(Context myContext, Activity myActivity, Handler myHandler) {
        IOIOHandler = myHandler;
        helper_.create();
    }

    protected void destroy() {
        helper_.destroy();
    }

    protected void start() {
        helper_.start();
    }

    protected void stop() {
        helper_.stop();
    }

    protected void restart() {
        helper_.restart();
    }

    class Looper extends BaseIOIOLooper {

        @Override
        protected void setup() throws ConnectionLostException {

        }

        @Override
        public void loop() throws ConnectionLostException, InterruptedException {
            Message myVoltageMessage = new Message();
            Bundle myVoltageBundle = new Bundle();
            myVoltageBundle.putFloat("myvoltage", AI1.getVoltage());
            myVoltageMessage.setData(myVoltageBundle);
            IOIOHandler.sendMessage(myVoltageMessage);
        }
    }

    @Override
    public IOIOLooper createIOIOLooper(String connectionType, Object extra) {
        return new Looper();
    }


}

Finally the Fragment that contains my GraphView's updateGraphUI method.:

public void updateGraphUI(float newReading) {
    final double newReadingDouble = newReading;
        mTimer2 = new Runnable() {

            @Override
            public void run() {
                graphLoopCounter++;

                if (globalData.isLiveUpdatingEnabled()) {
                alarmCheck(newReadingDouble);
                globalData.setGraph2LastXValue(globalData
                        .getGraph2LastXValue() + 1d);
                globalData.getGraphViewSeries().appendData(
                        new GraphViewData(globalData.getGraph2LastXValue(),
                                newValue), true, 1000);
                if (globalData.getGraphPeak() < newReadingDouble) {
                    globalData.setGraphPeak(newReadingDouble);
                    graphPeak.setText("Graph Peak: "
                            + Double.toString(newReadingDouble));
                }


                    avgSum += globalData.getLatestGraphData();
                        graphAvg.setText("Graph Avg: " + Double.toString(avgSum/graphLoopCounter));


                mHandler.postDelayed(this, 100);
                }
            }
        };
        mHandler.postDelayed(mTimer2, 100);

}

where mHandler is a handler in my Fragment's implementation. and both mTimer1 and mTimer2 are Runnables.

my problem is; no matter what I try to do data is not sent to my GraphView and it never updates...


Solution

  • In MainActivity you name your Handler instance "myHandler" but then you pass your MyLooper class "_myIOIOHandler".