2017-11-21 1 views
0

나는 이것에 대한 답을 찾기 위해 노력했지만 아직 행운을 얻지 못했기 때문에 나는 단지 물어볼 것이라고 생각했습니다.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> 
+0

그래서 문제가 무엇입니까? 문자열을 double로 변환하고 싶습니까? 'double yourLocDouble = Double.parseDouble (yourLocationString)'을 시도 했습니까? –

+0

'어트랙션'클래스에 위도와 경도를 두 배로 저장하는 것이 좋습니다. 앱에 고정식 레스토랑이 있습니까? 또는 앞으로 더 추가 할 수 있기를 원하십니까? 나중에 있다면 레스토랑 데이터를 저장하기 위해 어떤 종류의 데이터베이스를 사용해야합니다. –

답변

0

, 내가 필요 정확히 근무 parseDouble 제안을 시도했다.

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

      String lat = getString(attractions.getLatitude()); 
      latitude = Double.parseDouble(lat); 
      String lon = getString(attractions.getLongitude()); 
      longitude = Double.parseDouble(lon); 

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

이 코드는 Restaurants Fragment에 추가되었습니다. 도와 주신 모든 분들께 감사드립니다!

0

당신이 저장하려고 노력 해요 /를 얻을 더블과 스트링의 좌표? 어쨌든 두 명의 건축업자를 만들려고합니다. 하나의 StringBuilder로 지리 좌표를 작성하고 Uri 빌더를 사용하여 Uri을 작성한 다음 활동을 확인할 수 있는지 확인한 다음 시작하십시오. 요아킴 Huet의 제안에 따르면

StringBuilder geoString = new StringBuilder(); 
geoString.append(lat) 
    .append(",") 
    .append(long); 

Uri.Builder builder = new Uri.Builder(); 
builder.scheme("geo") 
    .path(geoString); 
Uri addressUri = builder.build(); 
Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setData(addressUri); 
if(intent.resolveActivity(getPacketManager()) != null){ 
    startActivity(intent); 
} 

`

관련 문제