0

私は私の中にある を持ってHandlerMainActivityます。これは、のレイアウト内にある のHandler1 つの UI を更新する必要があります。FragmentMainActivity

次に、ループを持つ別のクラスがあります。このクラスを作成したら、それを呼び出してmyLooper何らかの計算を行い、ループ内の情報をHandlermyに送信しますMainActivity。次に、そのデータを、GraphViewMainActivityのフラグメント内に実装した に更新します。

フロー: ステップ 1: でデータを計算しますMyLooper。ステップ 2: MainActivitymy 経由で にデータを送信しますHandler。ステップ 3:送信されたデータを更新HandlerMainActivityます。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();
    }


}

最後に、FragmentGraphViewupdateGraphUIメソッドを含む .:

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

}

どこに myの実装のmHandlerハンドラーがあります。とはFragment両方ともmTimer1sです。mTimer2Runnable

私の問題は; 何をしようとしても、データはGraphViewに送信されず、更新されません...

4

1 に答える 1

1

MainActivity では、Handler インスタンスに「myHandler」という名前を付けますが、MyLooper クラスに「_myIOIOHandler」を渡します。

于 2014-05-23T19:23:24.577 に答える