Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 列表航路点=新的ArrayList(); 试一试{ JSONObject JSONObject=新的JSONObject(json); JSONArray航路点JSONArray=jsonObject.getJSONObject(“msg”).getJSONArray(“航路点”); 对于(int i=0;i_Java_Android_Spinner - Fatal编程技术网

Java 列表航路点=新的ArrayList(); 试一试{ JSONObject JSONObject=新的JSONObject(json); JSONArray航路点JSONArray=jsonObject.getJSONObject(“msg”).getJSONArray(“航路点”); 对于(int i=0;i

Java 列表航路点=新的ArrayList(); 试一试{ JSONObject JSONObject=新的JSONObject(json); JSONArray航路点JSONArray=jsonObject.getJSONObject(“msg”).getJSONArray(“航路点”); 对于(int i=0;i,java,android,spinner,Java,Android,Spinner,最后将它们放入MainActivity类 public class MainActivity extends AppCompatActivity { private Spinner wayPointSpinner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVie

最后将它们放入MainActivity类

public class MainActivity extends AppCompatActivity {

    private Spinner wayPointSpinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        wayPointSpinner = findViewById(R.id.spinner_way_point);

        // This is the json string returned from server.
        String jsonString = "{\"topic\": \"/waypoints\", \"msg\": {\"waypoints\": [{\"header\": {\"stamp\": {\"secs\": 0, \"nsecs\": 0}, \"frame_id\": \"map\", \"seq\": 0}, \"pose\": {\"position\": {\"y\": -140.901899144836, \"x\": 44.36469369653882, \"z\": 0.0}, \"orientation\": {\"y\": 0.0, \"x\": 0.0, \"z\": 0.5062775131960053, \"w\": 0.8623706161692133}}, \"name\": \"A\"}, {\"header\": {\"stamp\": {\"secs\": 0, \"nsecs\": 0}, \"frame_id\": \"map\", \"seq\": 0}, \"pose\": {\"position\": {\"y\": -102.30524463734227, \"x\": 51.282396271771304, \"z\": 0.0}, \"orientation\": {\"y\": 0.0, \"x\": 0.0, \"z\": 0.6926372547579506, \"w\": 0.721286096719859}}, \"name\": \"B\"}]}, \"op\": \"publish\"}";

        // This will convert the json string to list of WayPoint
        List<WayPoint> wayPoints = getWayPointListFromJsonString(jsonString);

        // Create adapter for spinner
        SpinnerAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, wayPoints);

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

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

                // Process position here
                Position position = wayPoint.pose.position;

                // Process orientation here
                Orientation orientation = wayPoint.pose.orientation;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }
}
public类MainActivity扩展了AppCompatActivity{
专用微调器航路点微调器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wayPointSpinner=findViewById(R.id.spinner\u way\u point);
//这是从服务器返回的json字符串。
“道路点”以下:“,,,,,,,“msg\““,,,,,,,,,,,,,,,,,,,“道路点”以下::,,,,,,“表头”标题:::“,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,5062775131960053,\“w\“一名名名:““,”标题:::“““邮票”教师::“:,“邮票”教师:::““secs”教师:0,“非政政政政政政政司”教师:0,,“非政政政界人士::0 0 0 0,”0.862370616161616161613 3 3万万万万万万万人:::,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,79506,\'w\':0.721286096719859},\'name\':\'B\'},\'op\':\“发布\“}”;
//这将把json字符串转换为航路点列表
List wayPoints=getWayPointListFromJsonString(jsonString);
//为微调器创建适配器
SpinnerAdapter=new ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,航路点);
wayPointSpinner.setAdapter(适配器);
wayPointSpinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共id(AdapterView父级、视图、int p、长id){
航路点航路点=(航路点)父项getItemAtPosition(p);
//这里的进程位置
位置位置=航路点.pose.Position;
//这里的流程定位
方向=航路点。姿态。方向;
}
@凌驾
未选择公共无效(AdapterView父级){
}
});
}
}

Hi!感谢您提供示例代码。但是您能否解释一下这段代码,比如我说的“arresult”、“arr_akun”是什么意思?我还是个初学者。谢谢!@yenbico arresult是来自json对象的数组。例如:result:[{a,B,C,D}]。arr_akun是字符串数组。JSONArray arresult=新的JSONArray(result);string[]arr_akun=new String[arresult.length()];要完成任务,有很多工作要做。首先,您需要打开到服务器的连接以从服务器获取数据(在本例中为JSON字符串)。其次,您必须编写代码将JSON字符串解析到模型类。最后,将其填充到spinner中。@Nhấ是的,我可以从服务器获取数据。我已经完成了数据序列化。我只是无法将它们显示到我的微调器。所以你有一个JSON字符串,你需要在微调器上显示,对吗?@Nhấ我明白了,吃完午饭后我会帮你的
public class Pop extends Activity {

private SubscribedData<WaypointList> waypointlistData;
private RosbridgeListener rosbridge;
Spinner spin;
String json;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.popup);

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

    /** publisher definition with its type of message**/
    waypointlistData = new SubscribedData<>();
    final Type waypointlistType = new TypeToken<SubscribedData<WaypointList>>() {
    }.getType();


    /** A method to parse a jsonstring to waypoint list **/
    private List<Waypoint> getWayPointListFromJsonString(String json){
        List<Waypoint> wayPoints = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(json);
            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");

                // Parse pose to get position and orientation;
                JSONObject poseJsonObject = wayPointJsonObject.getJSONObject("pose");

                // Parse position form pose
                JSONObject positionJsonObject = poseJsonObject.getJSONObject("position");
                Position position = new Position();
                position.x = positionJsonObject.getFloat("x");
                position.y = positionJsonObject.getFloat("y");
                position.z = positionJsonObject.getFloat("z");

                // Parse orientation from pose.
                JSONObject orientationJsonObject = poseJsonObject.getJSONObject("orientation");
                Orientation orientation = new Orientation();
                orientation.x = orientationJsonObject.getFloat("x");
                orientation.y = orientationJsonObject.getFloat("y");
                orientation.z = orientationJsonObject.getFloat("z");
                orientation.w = orientationJsonObject.getFloat("w");

                Pose pose = new Pose();
                pose.position = position;
                pose.orientation = orientation;

                Waypoint wayPoint = new Waypoint();
                wayPoint.name = name;
                wayPoint.pose = pose;
                wayPoints.add(wayPoint);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return wayPoints;
    }

    // This is the json string returned from server.
    String jsonString = "{\"topic\": \"/waypoints\", \"msg\": {\"waypoints\": [{\"header\": {\"stamp\": {\"secs\": 0, \"nsecs\": 0}, \"frame_id\": \"map\", \"seq\": 0}, \"pose\": {\"position\": {\"y\": -140.901899144836, \"x\": 44.36469369653882, \"z\": 0.0}, \"orientation\": {\"y\": 0.0, \"x\": 0.0, \"z\": 0.5062775131960053, \"w\": 0.8623706161692133}}, \"name\": \"A\"}, {\"header\": {\"stamp\": {\"secs\": 0, \"nsecs\": 0}, \"frame_id\": \"map\", \"seq\": 0}, \"pose\": {\"position\": {\"y\": -102.30524463734227, \"x\": 51.282396271771304, \"z\": 0.0}, \"orientation\": {\"y\": 0.0, \"x\": 0.0, \"z\": 0.6926372547579506, \"w\": 0.721286096719859}}, \"name\": \"B\"}]}, \"op\": \"publish\"}";

    // This will convert the json string to list of WayPoint
    List<Waypoint> wayPoints = getWayPointListFromJsonString(jsonString);

    // Create adapter for spinner
    SpinnerAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, wayPoints);

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

            // Process position here
            Position position = wayPoint.pose.position;

            // Process orientation here
            Orientation orientation = wayPoint.pose.orientation;
        }

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

        /** a set of the waypoints popup window **/
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

        int width = dm.widthPixels;
        int height = dm.heightPixels;

        getWindow().setLayout((int) (width * .6), (int) (height * .6));


}
 for (int i = 0; i < arrResult.length(); i++) {
     arr_akun[i] = "string dynamically";
 }

 ArrayAdapter<String> adapter_akun = new ArrayAdapter<>(context, R.layout.spinner_akun_item, arr_akun);
 spinner.setAdapter(adapter_akun);

 spinner_akun.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
     // do something to get name
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
class WayPoint {
    String name;
    Pose pose;

    @NonNull
    @Override
    public String toString() {
        return name;
    }
}

class Pose {
    Position position;
    Orientation orientation;
}

class Position {
    double x, y, z;
}

class Orientation {
    double x, y, z, w;
}
private List<WayPoint> getWayPointListFromJsonString(String json) {
    List<WayPoint> wayPoints = new ArrayList<>();

    try {
        JSONObject jsonObject = new JSONObject(json);
        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");

            // Parse pose to get position and orientation;
            JSONObject poseJsonObject = wayPointJsonObject.getJSONObject("pose");

            // Parse position form pose
            JSONObject positionJsonObject = poseJsonObject.getJSONObject("position");
            Position position = new Position();
            position.x = positionJsonObject.getDouble("x");
            position.y = positionJsonObject.getDouble("y");
            position.z = positionJsonObject.getDouble("z");

            // Parse orientation from pose.
            JSONObject orientationJsonObject = poseJsonObject.getJSONObject("orientation");
            Orientation orientation = new Orientation();
            orientation.x = orientationJsonObject.getDouble("x");
            orientation.y = orientationJsonObject.getDouble("y");
            orientation.z = orientationJsonObject.getDouble("z");
            orientation.w = orientationJsonObject.getDouble("w");

            Pose pose = new Pose();
            pose.position = position;
            pose.orientation = orientation;

            WayPoint wayPoint = new WayPoint();
            wayPoint.name = name;
            wayPoint.pose = pose;
            wayPoints.add(wayPoint);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return wayPoints;
}
public class MainActivity extends AppCompatActivity {

    private Spinner wayPointSpinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        wayPointSpinner = findViewById(R.id.spinner_way_point);

        // This is the json string returned from server.
        String jsonString = "{\"topic\": \"/waypoints\", \"msg\": {\"waypoints\": [{\"header\": {\"stamp\": {\"secs\": 0, \"nsecs\": 0}, \"frame_id\": \"map\", \"seq\": 0}, \"pose\": {\"position\": {\"y\": -140.901899144836, \"x\": 44.36469369653882, \"z\": 0.0}, \"orientation\": {\"y\": 0.0, \"x\": 0.0, \"z\": 0.5062775131960053, \"w\": 0.8623706161692133}}, \"name\": \"A\"}, {\"header\": {\"stamp\": {\"secs\": 0, \"nsecs\": 0}, \"frame_id\": \"map\", \"seq\": 0}, \"pose\": {\"position\": {\"y\": -102.30524463734227, \"x\": 51.282396271771304, \"z\": 0.0}, \"orientation\": {\"y\": 0.0, \"x\": 0.0, \"z\": 0.6926372547579506, \"w\": 0.721286096719859}}, \"name\": \"B\"}]}, \"op\": \"publish\"}";

        // This will convert the json string to list of WayPoint
        List<WayPoint> wayPoints = getWayPointListFromJsonString(jsonString);

        // Create adapter for spinner
        SpinnerAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, wayPoints);

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

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

                // Process position here
                Position position = wayPoint.pose.position;

                // Process orientation here
                Orientation orientation = wayPoint.pose.orientation;
            }

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