gpt4 book ai didi

java - 将数据从另一个类传递给处理程序

转载 作者:行者123 更新时间:2023-12-01 13:05:32 25 4
gpt4 key购买 nike

我的 MainActivity 中有一个 Handler。此 Handler 必须更新 MainActivity 布局内我的一个 Fragment 的 UI。

然后我有一个单独的类,它有一个循环;当我创建此类时,我们将其命名为 myLooper,它会进行某种计算,然后将循环内的信息发送到我的 MainActivity 中的 Handler >。然后,它将将该数据更新到我在 MainActivity 的 Fragment 中实现的 GraphView

流程:步骤1:在MyLooper上计算数据。第 2 步:通过我的 Handler 将数据发送到 MainActivity。第 3 步:让我的 Handler 使用发送的数据更新 MainActivityFragmentGraphView

相关代码:

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


}

最后是包含我的 GraphViewupdateGraphUI 方法的 Fragment。:

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

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

最佳答案

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

关于java - 将数据从另一个类传递给处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23297951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com