Java 将数据动态填充到微调器中
我目前一直在思考如何向微调器显示数据。因此,基本上我使用Websocket接收数据并在UI线程上运行它。我的问题是,我的微调器中没有显示数据列表 这是我的密码:Java 将数据动态填充到微调器中,java,android,websocket,Java,Android,Websocket,我目前一直在思考如何向微调器显示数据。因此,基本上我使用Websocket接收数据并在UI线程上运行它。我的问题是,我的微调器中没有显示数据列表 这是我的密码: WayPointData = new SubscribedData<>(); final Type WayPointType = new TypeToken<SubscribedData<WayPoint>>() { }.getType(); /** an ID f
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) {
}
});
WayPointData=newsubscribeddata();
最终类型WayPointType=新类型令牌(){
}.getType();
/**微调器的ID**/
spin=(微调器)findViewById(R.id.Spinner);
final SpinnerAdapter=new ArrayAdapter(Pop.this、android.R.layout.simple\u微调器\u项);
spin.setAdapter(适配器);
rosbridge=新的RosbridgeListener(“ws://10.24.204.231:9090”);
setOnDataReceivedListener(新的RosbridgeMessageListener(){
/**
*一个正在运行的线程,当建立连接时,主题的数据将序列化和反序列化java对象
*到(和从)JSON。
*@param msg
*/
@凌驾
公共无效onDataReceived(最终字符串消息){
试一试{
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
试一试{
WayPointData=new Gson().fromJson(消息,WayPointType);
JSONObject JSONObject=新的JSONObject();
JSONArray航路点JSONArray=jsonObject.getJSONObject(“msg”).getJSONArray(“航路点”);
对于(int i=0;i
谢谢所有能帮助我的人 初始化适配器时添加阵列:
final SpinnerAdapter adapter = new ArrayAdapter<String>(Pop.this, android.R.layout.simple_spinner_item, yourStringArray);
final SpinnerAdapter adapter=new ArrayAdapter(Pop.this,android.R.layout.simple\u spinner\u项,yourStringArray);
如果以后进行更改(将从服务器接收列表),只需将以前使用的数组与新数据一起设置,并使用
适配器。notifyDataSetChanged()
在初始化适配器时添加数组:
final SpinnerAdapter adapter = new ArrayAdapter<String>(Pop.this, android.R.layout.simple_spinner_item, yourStringArray);
final SpinnerAdapter adapter=new ArrayAdapter(Pop.this,android.R.layout.simple\u spinner\u项,yourStringArray);
如果您稍后进行更改(将从服务器接收列表),只需将以前使用的数组与新数据一起设置,并使用
适配器.notifyDataSetChanged()
这里我可以看到您没有将json字符串
传递给JSONObject
,这就是为什么您的主json对象是空的json对象的原因
您应该将JSON字符串
传递给JSONObject
参数,如下所示
JSONObject jsonObject = new JSONObject(msg); // here you have to add your json string in JSONObject parameter
希望它能帮助您。在这里,我可以看到您没有将json字符串
传递给JSONObject
,这就是为什么您的主json对象是空的json对象
您应该将JSON字符串
传递给JSONObject
参数,如下所示
JSONObject jsonObject = new JSONObject(msg); // here you have to add your json string in JSONObject parameter
希望它能帮助您。您有什么问题吗?@pskink基本上是将我的数据显示给我的微调器。我可以接收数据,但无法将其显示到微调器。我想我的代码中缺少了一些内容。您的SpinnerAdapter适配器
为空:您没有向onDataReceived()中的适配器添加任何数据。
method@pskink. 我明白了,如果数据来自我的服务器(rosbridge),我将如何向适配器添加数据?使用ArrayAdapter\add()
方法?为什么不检查官方文件?你有什么问题?@pskink基本上是将我的数据显示给我的微调器。我可以接收数据,但无法将其显示到微调器。我想我的代码中缺少了一些内容。您的SpinnerAdapter适配器
为空:您没有向onDataReceived()中的适配器添加任何数据。
method@pskink. 我明白了,如果数据来自我的服务器(rosbridge),我将如何向适配器添加数据?使用ArrayAdapter\add()
方法?为什么不查看官方文档呢?如果我的字符串数组来自我的服务器(rosbridge),我如何在不硬编码的情况下设置它呢?所以只有在接收数据时才初始化并设置适配器。Spinner在没有适配器的情况下不会崩溃,它只是不会显示任何选项。如果我的字符串数组来自我的服务器(rosbridge),我如何在不硬编码的情况下设置它?因此,只有在接收数据时才初始化并设置适配器。没有适配器,微调器不会崩溃,它只是不会显示任何选项。