我的程序是这样的:我将数据 api 的响应传递给第二个活动,即 ListActivity。而且我在做List映射的时候把所有的缩略图都一一下载了。它起作用了,但是当它下载缩略图时,用户界面冻结了。我试图用 AsycTask 来做,但没有用。
这是我的代码:第一个活动
public void Search(View view){
String searchText;
searchText = searchTarget.getText().toString();
searchText= searchText.replaceAll(space,searchOr);
if(searchText==null||searchText==""){
Toast.makeText(Login.this, "Please enter at least one keyword", Toast.LENGTH_LONG).show();
}
else{
try {
//AsyncTask code here
String SearchLink=searchTextStart+searchText+searchTextMid+page+searchTextEnd;
new DownloadTask().execute(SearchLink);
Log.v("Search checking ", SearchLink);
} catch(Exception ex) {
//your timeout code
Toast.makeText(this, "No drivers found, network problem!", Toast.LENGTH_LONG).show();
}
this.pd = ProgressDialog.show(this, "Working..", "Searching...", true, false);
}
}
第二次活动
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getIntent().getExtras();
arrayLength= bundle.getInt("indexToList");
videoInfo = bundle.getStringArray("videoInfo");
mData=getData();
MyAdapter adapter = new MyAdapter(this);
setListAdapter(adapter);
}
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Log.v("In videoInfo List","length"+arrayLength);
Map<String, Object> map;
for(int j=0, i=0 ; j<arrayLength; j++, i++){
map = new HashMap<String, Object>();
map.put("uploaded", videoInfo[j]);
j++;
map.put("category", videoInfo[j]);
j++;
map.put("title", videoInfo[j]);
j++;
map.put("description", videoInfo[j]);
j++;
try {
Log.i("Check bitmap","before");
//new DownloadTask().execute(videoInfo[j]);
//VideoList.this.pd = ProgressDialog.show(VideoList.this, "Working..", "Searching...", true, false);
Log.i("Check bitmap","after"+videoInfo[j]);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(videoInfo[j]).getContent());
map.put("thumbnail", bitmap);
}
catch (Exception e){
Toast.makeText(VideoList.this, "Network problem!", Toast.LENGTH_LONG).show();
}
//map.put("thumbnail",thumbnail[i]);
j++;
map.put("youtube_link", videoInfo[j]);
j++;
map.put("rtsp", videoInfo[j]);
Log.v("In videoInfo List","rtsp: "+videoInfo[j]);
j++;
float a = Float.parseFloat(videoInfo[j]);
int b=(int)a;
int sec = b % 60;
int min = b/60;
String duration;
if (sec<10)
{ duration = min+" : 0"+sec;}
else
{duration = min+" : "+sec;}
videoInfo[j] = duration;
map.put("duration", videoInfo[j]);
Log.v("In videoInfo List","duration: "+videoInfo[j]);
list.add(map);
}
Log.v("In videoInfo List","list: "+list.size());
return list;
}
对不起我的英语,如果你不明白我写了什么,请告诉我。谁能帮帮我??