2012-11-10 6 views
2

doInBackground 메서드에서 JSONArray를 읽고 사용자 지정 Items (ArrayList)의 ArrayList를 반환하는 AsyncTask를 만들었습니다. 이것 전에 onPostExecute 메서드에서 AsynchronousTask에있는 ArrayList를 Main 스레드의 다른 ArrayList로 옮긴다.하지만 AsyncTask가 끝나지 않고 여전히 작동하고 있다고 생각한다. 여기AsyncTask는 onPostExecute 메서드로 끝나지 않습니다.

AsyncTask를 : 여기에

private class ReadJSONTask extends AsyncTask<Void, Void, ArrayList<Item>>{ 

     @Override 
     protected ArrayList<Item> doInBackground(Void... params) { 
      // TODO Auto-generated method stub 
      ArrayList<Item> auxList = new ArrayList<Item>(); 
      LoadData(auxList); //Method that reads JSON and load information in the ArrayList 
      return auxList; // Return ArrayList 
     } 

     @Override 
     protected void onPostExecute(ArrayList<Item> result) { 
      // TODO Auto-generated method stub 

      Log.i("OnPostExecute", String.valueOf(result.size())); //The size of the array 
      listMain = result; // Move the data of the AsyncTask to the main Thread 

      Log.i("OnPostExecute", String.valueOf(listaComic.size())); //The size of the ArrayList I use in the Main Thread 
     } 

    } 

하여 메인 스레드에서 AsyncTask를 위해 전화 : 내가 onPostExecute의 모든 로그인 볼 로그에서

if (isOnline()){ //Return true if there is internet connection 
    ReadJSONTask task = new ReadJSONTask(); 
    task.execute(); 

    Log.i("Main Thread - listMain Size", String.valueOf(listMain.size())); //Never executed 

    //This for loop its only for debug purposes, never executed 
    for (Item item : listMain){ 
     Log.i("Items", item.toString()); 
    } 
} 

여기에 내 코드를 삽입() 메서드가 인쇄되지만 주 스레드에서는 아무것도 표시되지 않습니다.

나는 그것을 해결하기 위해 오류이고, 내가 이일 거기에 노력하고있다 및 StackOverflow의 여기, 포럼에서 검색 모르고 내가 그 문제를 해결 할 수 없습니다 S

답변

4

을 이름 AsyncTask을 나타냅니다으로 비동기식이지만 어떤 이유로 async 작업이 종료되지 않으면 execute()이 차단 될 것으로 예상됩니다. 이는 잘못된 것입니다. 귀하의 코드가 잘 작동하고 listMain이 비어있을 뿐이고 비동기 작업이 아직 완료되지 않았기 때문에 execute()이 발생하면 asynctask가 발생하고 for은 아무 것도 표시하지 않습니다. 당신은 비동기 태스크가 "메인 쓰레드"가 완료되었다는 것을 알 수 있도록 애플리케이션 로직을 재 작업해야합니다. 나는. for 루프를 별도의 메서드로 이동하고 onPostExecute()에서 호출하십시오.

+0

Asynctask에 대한 또 다른 알고리즘이 개선되었으며 지금은 작동합니다.이 강의를 가르쳐 주셔서 감사합니다.이 기능은 첫 번째 AsyncTask이고 이에 대한 올바른 접근 방법이 없습니다. 감사 :) – SekthDroid

관련 문제