如何使用Spinner从显示文本中获得不同的返回值?

时间:2018-07-10 14:19:46

标签: android spinner

public void onNetworkSuccess(List<Mission> missionList) {

            List<LocationSpinnerObject> LocationsList = new ArrayList<>();
            LocationsList.add(getCurrentLocation());
            for(Mission mission : missionList){



            }
            LocationsSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                }

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

            });    

我想从我的任务对象中显示很少的数据,例如lang,lat和address。在微调器中,我想显示地址,当我选择一个地址时,地图动画将更新到该位置(意味着lat和lang将成为返回值)

我该怎么办? 谢谢:)

1 个答案:

答案 0 :(得分:0)

让我们假设您的Mission对象包含latlangaddress

public void onNetworkSuccess(List<Mission> missionList) {
    List<String> LocationsList = new ArrayList<>();
    LocationsList.add(getCurrentLocation());
    for(Mission mission : missionList) {
        LocationList.add(mission.getAddress()); // This is will show only the address on the spinner.    
    }

    LocationsSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Mission mission = missionList.get(position); // Retrieve your mission with the selected item position.
            float latitude = mission.getLatitude();
            float longitude = mission.getLongitude(); 
        }

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

        }
    });    
}