将数据传递给另一个类的处理程序

时间:2014-04-25 16:02:54

标签: java android android-fragments handler ioio

我的Handler中有一个MainActivity。此Handler必须更新Fragment布局中MainActivity之一的用户界面。

然后我有一个单独的类,它有一个循环;当我创建这个类时,让我们调用它myLooper它会进行某种计算,然后将循环内部的信息发送到Handler中的MainActivity。然后,这会将该数据更新为我在GraphView的片段中实现的MainActivity

FLOW: 第1步:计算MyLooper上的数据。 第2步:通过我的MainActivity将数据发送到Handler。 第3步:让我的Handler更新MainActivity FragmentGraphView包含已发送的数据。

相关代码:

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课程:

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();
    }


}

最后是包含Fragment GraphView方法的updateGraphUI。:

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);

}

其中mHandler是我Fragment实现中的处理程序。 mTimer1mTimer2都是Runnable s。

我的问题是;无论我尝试做什么,数据都不会发送到我的GraphView,它永远不会更新......

1 个答案:

答案 0 :(得分:1)

在MainActivity中,您将Handler实例命名为“myHandler”,但随后将MyLooper类命名为“_myIOIOHandler”。