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

时间:2019-10-02 08:50:00

标签: java android websocket

我目前在如何将数据显示给微调器方面陷入困境。因此,基本上我正在使用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) {
        }
    });

感谢任何可以帮助我的人!

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

在这里我可以看到您没有将json string传递给JSONObject,这就是为什么您的主要JSON对象是空的json object

您应将JSON string传递给JSONObject参数,如下所示。

JSONObject jsonObject = new JSONObject(msg); // here you have to add your json string in JSONObject parameter

希望它对您有帮助。