내 응용 프로그램은 주소가 포함 된 사용자가 선택한 파일을 읽은 다음 지오 코딩 완료시 mapview에 표시합니다. 정지 응용 프로그램을 피하기 위해 가져 오기 및 지오 코딩은 AsyncTask에서 수행됩니다.doInBackground 메서드에서 gui 연산을 수행하는 방법은 무엇입니까?
public class LoadOverlayAsync extends AsyncTask<Uri, Integer, StopsOverlay> {
Context context;
MapView mapView;
Drawable drawable;
public LoadOverlayAsync(Context con, MapView mv, Drawable dw)
{
context = con;
mapView = mv;
drawable = dw;
}
protected StopsOverlay doInBackground(Uri... uris)
{
StringBuilder text = new StringBuilder();
StopsOverlay stopsOverlay = new StopsOverlay(drawable, context);
Geocoder geo = new Geocoder(context, Locale.US);
try
{
File file = new File(new URI(uris[0].toString()));
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null)
{
StopOverlay stopOverlay = null;
String[] tempLine = line.split("~");
List<Address> results = geo.getFromLocationName(tempLine[4] + " " + tempLine[5] + " " + tempLine[7] + " " + tempLine[8], 10);
if (results.size() > 0)
{
Toast progressToast = Toast.makeText(context, "More than one yo", 1000);
progressToast.show();
}
else if (results.size() == 1)
{
Address addr = results.get(0);
GeoPoint mPoint = new GeoPoint((int)(addr.getLatitude() * 1E6), (int)(addr.getLongitude() * 1E6));
stopOverlay = new StopOverlay(mPoint, tempLine);
}
if (stopOverlay != null)
{
stopsOverlay.addOverlay(stopOverlay);
}
//List<Address> results = geo.getFromLocationName(locationName, maxResults)
}
} catch (URISyntaxException e) {
showErrorToast(e.toString());
//e.printStackTrace();
} catch (FileNotFoundException e) {
showErrorToast(e.toString());
//e.printStackTrace();
} catch (IOException e) {
showErrorToast(e.toString());
//e.printStackTrace();
}
return stopsOverlay;
}
protected void onProgressUpdate(Integer... progress)
{
Toast progressToast = Toast.makeText(context, "Loaded " + progress.toString(), 1000);
progressToast.show();
}
protected void onPostExecute(StopsOverlay so)
{
//mapView.getOverlays().add(so);
Toast progressToast = Toast.makeText(context, "Done geocoding", 1000);
progressToast.show();
}
protected void showErrorToast(String msg)
{
Toast Newtoast = Toast.makeText(context, msg, 10000);
Newtoast.show();
}
}
그러나 지오 코드가 실패하면 사용자가 주소를 편집 할 수있는 대화 상자 팝업이 필요합니다. doInBackground에서 gui 메소드를 호출해야합니다. 이 좋은 해결 방법은 무엇입니까?
한 가지 더, 나는 라인 데이터 (tha t는 디코딩 될 수 없습니다) String [] tempLine = line.split ("~"); 대화 상자가 호출되는 메소드. 지오 코딩이 실패한 배열을 만들려고합니다. 그런 다음 사용자를 편집 할 수 있도록 onPostExecute에 나열하십시오. –
doInBackground에 mGeocodingOutputLines과 member 필드를 설정할 수도 있습니다. 그런 다음 onPostExecute를 표시합니다. 나는 그것이 좋을 것이라고 생각한다. – louielouie