gpt4 book ai didi

java - 动态地将数据填充到微调器中

转载 作者:行者123 更新时间:2023-11-30 05:01:19 24 4
gpt4 key购买 nike

我目前卡在如何将我的数据显示到我的微调器上。所以基本上我使用 Websocket 来接收数据并在我的 UI 线程上运行它我的问题是我的微调器中没有显示数据列表。

这是我的代码:

    WayPointData = new SubscribedData<>();
final Type WayPointType = new TypeToken<SubscribedData<WayPoint>>() {
}.getType();


/** an ID for the spinner **/
spin = (Spinner) findViewById(R.id.spinner);

final SpinnerAdapter adapter = new ArrayAdapter<String>(Pop.this, android.R.layout.simple_spinner_item);
spin.setAdapter(adapter);

rosbridge = new RosbridgeListener("ws://10.24.204.231:9090");
rosbridge.setOnDataReceivedListener(new RosbridgeMessageListener() {

/**
* a running thread that when the connection is made the data of the topic will serialize and deserialized java objects
* to (and from) JSON.
* @param msg
*/
@Override
public void onDataReceived(final String msg) {
try {
runOnUiThread( new Runnable() {
@Override
public void run() {
try {
WayPointData = new Gson().fromJson(msg,WayPointType);
JSONObject jsonObject = new JSONObject();
JSONArray wayPointJsonArray = jsonObject.getJSONObject("msg").getJSONArray("waypoints");
for (int i = 0; i < wayPointJsonArray.length(); i++) {
JSONObject wayPointJsonObject = wayPointJsonArray.getJSONObject(i);
// Parse name
String name = wayPointJsonObject.getString("name");
WayPoint wayPoint = new WayPoint();
wayPoint.name = name;

}
} catch (Exception e) {
e.printStackTrace();
}

}
});

/** a msg that will display once the data is received **/
Log.d("B9T", String.format("Received data: %s", msg));
} catch (Exception e) {
e.printStackTrace();
}

}
});

spin.setAdapter(adapter);
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view, int p, long id) {
WayPoint wayPoint = (WayPoint) parent.getItemAtPosition(p);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

感谢任何能帮助我的人!

最佳答案

在初始化适配器时添加数组:

final SpinnerAdapter adapter = new ArrayAdapter<String>(Pop.this, android.R.layout.simple_spinner_item, yourStringArray);

如果您稍后进行更改(将从服务器接收列表)只需设置您之前使用新数据的数组并使用 adapter.notifyDataSetChanged()

关于java - 动态地将数据填充到微调器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58198206/

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