私はこれに対する答えの日を探していますが、まだ運がなかったので、私はちょうど尋ねると思った。ArrayListから文字列を取得し、使用可能な緯度と経度に変換します
私は、私が取り組んできた小規模な地方の旅行アプリを持っていますが、機能の大半は動作していますが、私の中の各特定の項目から緯度と経度の情報を引き出す方法を考え出すことはできません配列リスト。
地図のアプリが両方ともハードコードされていると正しく機能していたので、私は今のところ、2倍の経度を持っていますが、ユーザーが選択した項目に従って動作させたいと考えています。
断片コード
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 yourLocDouble = Double.parseDouble(yourLocationString)'を試しましたか? –
「アトラクション」クラスに緯度と経度を2倍として保存することをお勧めします。あなたのアプリには固定レストランのセットがありますか?または、今後さらに追加できるようにしたいですか?後であれば、レストランデータを保存するために何らかの種類のデータベースを使用する必要があります。 –