我正在做我的第一個android應用程序。在其中一頁上,我需要使用REST服務連接到Drupal站點,檢索一些數據(視頻URL列表,它們的標題,描述等)並將其顯示在列表中。當我點擊列表時,我會轉到視頻的細節。等待異步任務完成
這是我想要繼續。 1°從Drupal站點獲取所有數據。 2°點擊列表中的某個項目時,將該視頻的詳細信息傳遞給下一個活動。
問題是這樣的: 當連接到Android 3+的互聯網時,你不能在主線程上這樣做,所以我不得不使用AsyncTask來獲取數據。這工作,但然後我想將視頻保存在ArrayList中,然後使用getVideos()或getVideo(index)函數訪問該列表。第一個填充列表,第二個在進入細節活動之前檢索數據。問題是,當我嘗試訪問視頻列表時,該列表尚未填充。
從技術上講,我並不真的需要/想要使用asynctask,但在主線程上連接到互聯網會引發錯誤,說它不是正確的做事方式。
這裏是我如何獲得錄像的簡化版本:
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
}
}
這是我要填寫我的活動視頻列表:
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));
}
所以,真正的問題是這樣的。有沒有更好的方法來解決這個問題,或者有沒有辦法等待列表被填充。
我最終選擇了這個解決方案(除了我不在postExecute中執行它,而是使用一個處理程序返回到VideoDaoImpl類)。 – Vilcoyote
另外,Android Query可能會讓人感興趣,它的工作原理非常簡單,太糟糕了,後來我發現它太晚了:http://code.google.com/p/android-query/ – Vilcoyote