我正在做我的第一个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