2012-07-31 4 views
0

AutoCompleteTextView가있는 앱이 있습니다. 모든 텍스트 변경 이벤트에서 앱은 웹으로 이동하여 인터넷에서 일부 콘텐츠를 검색하고 TextView에 대한 드롭 다운을 채 웁니다. 웹 콘텐트 읽기를 위해 AsyncTask를 사용했다. 그러나 콘텐츠가 수신되고 채워지기 전에 새 텍스트를 입력하면 이전 콘텐츠를 가져올 때까지 앱이 중단됩니다. 문제를 해결할 수있는 방법이 있습니까? 다음과 같이Android : AsyncTask가 예상대로 작동하지 않습니다.

내 AsyncTask를이

private class GetSuggestions extends AsyncTask<String, Void, String> { 

    @Override 
    protected String doInBackground(String... params) { 
     System.out.println("Suggestions Called()"); 
     doSearch(params[0]); // reads the web and populates the suggestions ArrayList 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     System.out.println("Adapter Called() " + suggestions.size()); 
     suggestionAdapter = new ArrayAdapter<String>(
       getApplicationContext(), R.layout.list, suggestions); 
     searchText.setAdapter(suggestionAdapter); 
    } 
} 

들으이다! Rahul.

+0

귀하의 경우에는 귀하가 성취하고자하는 바에 따라 다릅니다. 드롭 다운에있는 것이 무엇이든간에 'TextView'가 무엇이든, 그리고 항상 그 TextView'와 관련이 있다면, 프로세스를 멈추고 새로운 것으로 다시 시작합니다. 그렇지 않다면, 대기리스트를 만들 수 있고,'onPostExecute'가 그리스트를 검사하여 다음에 무엇이 있는지 확인할 수 있습니다. – Andy

답변

0

AsyncTask가 실행 중인지 확인할 수 있습니다.

public boolean isRunning() 
{ 
    if (_querymysqltask == null) return false; 
    if (_querymysqltask.getStatus() == AsyncTask.Status.FINISHED) return false; 
    else return true; 
} 

작업을 취소하여 새 검색으로 다시 시작하거나 종료 할 때까지 기다릴 수 있습니다.

0
if(task == null) 
{ 
    task = new GetSuggestions(); 
    task.execute(new String[] {word}); 
} 
else 
{ 
    task.cancel(true); 
    task = new GetSuggestions(); 
    task.execute(new String[] {word}); 
} 

작업을 취소하고 새로 입력 한 텍스트로 새 작업을 시작할 수 있습니다. 코드는 위와 유사합니다.

0

웹에서 데이터를 가져올 때까지 progressDialog를 표시 할 수 있습니다.

private ProgressDialog dialog = new ProgressDialog(HomeActivity.this); 

    /** progress dialog to show user that the backup is processing. */ 
    /** application context. */ 

    protected void onPreExecute() { 
     this.dialog.setMessage("Please wait"); 
     this.dialog.show(); 
    } 


    @Override 
    protected void onPostExecute(final Boolean success) { 

     if (dialog.isShowing()) { 
      dialog.dismiss(); 
     } 
    } 
0

doInBackground()에서 호출 된

doSearch(params[0]); // reads the web and populates the suggestions ArrayList 

doSearch()에, 그래서 '웹을 읽습니까, elemnets.only UI를 터치 안됩니다 '부분은 doInBackground()이고 ArrayList는 onPostExecute()에서 채 웁니다.

관련 문제