2012-10-21 7 views
0

내 응용 프로그램은 주소가 포함 된 사용자가 선택한 파일을 읽은 다음 지오 코딩 완료시 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 메소드를 호출해야합니다. 이 좋은 해결 방법은 무엇입니까?

답변

0

onPostExecute 메서드에서 처리해야합니다. 어쩌면 onPostExecute의 null 인수가 실패했음을 나타내므로이 경우 팝업 대화 상자가 나타납니다.

0

StopsOverlay의 결과 유형을 변경하지 않으려면 doInBackground에 일부 멤버 필드를 설정 한 다음 onPostExecute의 멤버 필드를 확인하고 그 시점에서 오류 UI를 표시하십시오. 이 안전하고 AsyncTask docs 당 권장 것을

참고 : 모든 콜백 호출은 다음과 같은 작업이 명시 적으로 동기화없이 안전한 방식으로 동기화되는 것을

AsyncTask를 보장합니다.

doInBackground (Params ...)의 멤버 필드를 설정하고 onProgressUpdate (Progress ...) 및 onPostExecute (Result)에서 참조하십시오.

예를 들어, 필드 선언 할 수 : doInBackground에, 당신이되도록 변경 다음

private boolean mTooManyResults = false; 

를 코드와 같은 : onPostExecute에서 다음

if (results.size() > 0) 
{ 
    mTooManyResults = true; 
} 

:

if (mTooManyResults) 
{ 
    // notify user about error 
    Toast progressToast = Toast.makeText(context, "More than one yo", 1000); 
    progressToast.show(); 
} else 
{ 
    // notify user about success 
    Toast progressToast = Toast.makeText(context, "Done geocoding", 1000); 
    progressToast.show(); 
} 
+0

한 가지 더, 나는 라인 데이터 (tha t는 디코딩 될 수 없습니다) String [] tempLine = line.split ("~"); 대화 상자가 호출되는 메소드. 지오 코딩이 실패한 배열을 만들려고합니다. 그런 다음 사용자를 편집 할 수 있도록 onPostExecute에 나열하십시오. –

+0

doInBackground에 mGeocodingOutputLines과 member 필드를 설정할 수도 있습니다. 그런 다음 onPostExecute를 표시합니다. 나는 그것이 좋을 것이라고 생각한다. – louielouie

관련 문제