첫 번째 Android 앱을 만들고 있습니다. 페이지 중 하나에서 REST 서비스를 사용하여 Drupal 사이트에 연결하고 일부 데이터 (비디오 URL 목록, 제목, 설명 등)를 검색하여 목록에 표시해야합니다. 목록을 클릭하면 동영상의 세부 정보로 이동합니다.asynchTask가 끝날 때까지 대기
다음은 진행 방법입니다. 1 ° drupal 사이트의 모든 데이터를 가져 오는 중입니다. 2 ° 목록의 항목을 클릭하면 해당 비디오의 세부 사항을 다음 활동으로 전달합니다.
문제는 다음과 같습니다. android 3+에서 인터넷에 연결할 때 주 스레드에서 실행할 수 없으므로 AsyncTask를 사용하여 데이터를 가져와야했습니다. 이 작업은 끝났지 만 비디오를 ArrayList에 저장 한 다음 getVideos() 또는 getVideo (index) 함수를 사용하여 해당 목록에 액세스하려고합니다. 첫 번째는 목록을 채우고 두 번째는 세부 활동으로 이동하기 전에 데이터를 검색합니다. 문제는 동영상 목록에 액세스하려고 할 때 목록이 채워지지 않았다는 것입니다.
기술적으로 나는 asynctask를 실제로 사용할 필요가 없거나 사용하고 싶지 않지만, 주 스레드에서 인터넷에 연결하는 것은 오류를 던져서 적절한 방법이 아니라고 말합니다.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videos_list);
//get all videos (not yet filled since faster than second thread)
List<Video> videos = videoDao.getVideos();
//get a list of all the titles to display as the list
List<String> formattedVideos = VideoHelper.formatVideosList(videos);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, formattedVideos);
ListView listView = (ListView) findViewById(R.id.videos_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new VideosListItemClickListener(this));
}
그래서 질문은 정말 이것이다 :
public class VideoDaoImpl {
private List<Video> videos ;
public VideoDaoImpl(){
videos = new ArrayList<Video>();
new VideoTask(...).execute(); //fetch the videos in json format and
//call function onRemoteVideoCallComplete using a handler
//in the onPostExecute() function of the asyncTask
}
public List<Video> getVideos() {
return videos;
}
public Video getVideo(int index){
return videos.get(index);
}
public onRemoteVideoCallComplete(JSONObject json) {
//transform Json into videos and add them to the videos arraylist
}
}
이 내가 내 활동에 동영상 목록을 입력하는 방법입니다 : 여기
내가 비디오를 얻는 방법에 대한 단순화 된 버전입니다 . 이것에 더 좋은 방법이 있을까, 또는리스트가 채워지기를 기다리는 방법이있다.
필자는 결국이 솔루션을 선택했습니다 (postExecute에서는 수행하지 않지만 VideoDaoImpl 클래스로 돌아가려면 처리기를 사용합니다). – Vilcoyote
또한 Android Query는 사람들에게 흥미로울 수 있습니다. 매우 간단하게 작동합니다. 나중에 너무 많이 발견되었습니다. http://code.google.com/p/android-query/ – Vilcoyote