나는 이것에 대한 답을 찾기 위해 노력했지만 아직 행운을 얻지 못했기 때문에 나는 단지 물어볼 것이라고 생각했습니다.ArrayList에서 문자열을 가져와 사용 가능한 위도와 경도로 변환합니다.
나는 작업하고있는 작은 지역 관광 앱을 가지고 있으며 대부분의 기능이 제대로 작동하지만 위도와 경도 정보를 가져 오는 방법을 알지 못한다. ArrayList.
두 제품 모두 하드 코딩 된 경우지도 앱이 제대로 작동하기 때문에 지금은 두 번 경도가 있지만 사용자가 선택한 항목에 따라 작동하고 싶습니다.
조각 코드
public class RestaurantsFragment extends Fragment {
private double latitude;
private double longitude;
public RestaurantsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.attraction_list, container, false);
// Create a list of restaurants
final ArrayList<Attractions> attraction = new ArrayList<Attractions>();
attraction.add(new Attractions(R.string.black_n_blue, R.string.black_n_blue_contact, R.string.black_n_blue_desc, R.drawable.bnb, R.string.slater_loc));
attraction.add(new Attractions(R.string.cardona, R.string.cardona_contact, R.string.cardona_desc, R.drawable.cardona, R.string.slater_loc));
attraction.add(new Attractions(R.string.athos, R.string.athos_contact, R.string.athos_desc, R.drawable.athos, R.string.slater_loc));
attraction.add(new Attractions(R.string.tanpopo, R.string.tanpopo_contact, R.string.tanpopo_desc, R.drawable.tanpopo, R.string.slater_loc));
// Create an {@link AttractionsAdapter}, whose data source is a list of {@link Attractions}s. The
// adapter knows how to create list items for each item in the list.
AttractionsAdapter adapter = new AttractionsAdapter(getActivity(), attraction, R.color.restaurants);
// Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
// There should be a {@link ListView} with the view ID called list, which is declared in the
// attraction_list.xml layout file.
ListView listView = (ListView) rootView.findViewById(R.id.list);
// Make the {@link ListView} use the {@link AttractionsAdapter} we created above, so that the
// {@link ListView} will display list items for each {@link Attractions} in the list.
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// Get the {@link Attractions} object at the given position the user clicked on
Attractions attractions = attraction.get(position);
latitude = attractions.getLocation();
longitude = -73.850338;
//Open map app to the address
String uri = String.format(Locale.ENGLISH, "geo:%f,%f", latitude, longitude);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
}
});
return rootView;
}
}
그리고 :
public class Attractions {
//Image reference source
private int mImageResourceId = NO_IMAGE_PROVIDED;
//Attraction name
private int mName;
//Attraction hours
private int mContact;
//Attraction description
private int mDescription;
//Attraction location
private int mLocation;
private static final int NO_IMAGE_PROVIDED = -1;
public Attractions(int name, int contact, int description, int imageResourceId, int location){
mImageResourceId = imageResourceId;
mName = name;
mContact = contact;
mDescription = description;
mLocation = location;
}
public Attractions(int name, int contact, int description, int location){
mName = name;
mContact = contact;
mDescription = description;
mLocation = location;
}
public boolean hasImage() {
return mImageResourceId != NO_IMAGE_PROVIDED;
}
public int getImageResourceId() {
return mImageResourceId;
}
public int getName() {
return mName;
}
public int getContact() {
return mContact;
}
public int getDescription() {return mDescription;}
public int getLocation() {return mLocation;}
@Override
public String toString() {
return "Word{" +
"mImageResourceId=" + mImageResourceId +
", mName='" + mName + '\'' +
", mContact='" + mContact + '\'' +
", mDescription='" + mDescription + '\'' +
", mLocation='" + mLocation + '\'' +
'}';
}
}
그리고 난 그냥 strings.xml의이 같은 형식이 있습니다
<string name="slater_loc" formatted="false">42.689319</string>
그래서 문제가 무엇입니까? 문자열을 double로 변환하고 싶습니까? 'double yourLocDouble = Double.parseDouble (yourLocationString)'을 시도 했습니까? –
'어트랙션'클래스에 위도와 경도를 두 배로 저장하는 것이 좋습니다. 앱에 고정식 레스토랑이 있습니까? 또는 앞으로 더 추가 할 수 있기를 원하십니까? 나중에 있다면 레스토랑 데이터를 저장하기 위해 어떤 종류의 데이터베이스를 사용해야합니다. –