0

My program is like this: I passed the data api's respopnse to the second activity which is a ListActivity. And I downloaded all the thumbnail one by one when I was doing the List mapping. It worked, but when it was downloading the thumbnail, the UI froze. I tried to do it using AsycTask but it didn't work.

here is my code: The first Activity

      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);
          } 
      }

The second Activity

    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;
}

Sorry for my English, if you don't understand what I've written pleas let me know. Could anyone help me??

crystalWing
  • 69
  • 1
  • 11
  • How have you implemented your logic ? Did you have a look at http://stackoverflow.com/questions/3803495/how-to-retrieve-youtube-thumbnails-using-google-api-client-library-for-java ? – Saurabh Aug 29 '12 at 14:24
  • I did. I am able to download and display the thumbnail in an imageView. But when it was downloading the thumbnail, the UI froze. I also know that this can be solved by using AsychTask. But if I put mData= getData(); in AsychTask, the program will crash. Anyway, Thanks for your help! – crystalWing Aug 29 '12 at 14:41
  • Can you post all the logic that you have written ? May be that will help in nailing down the exact issue – Saurabh Aug 29 '12 at 14:51

1 Answers1

0

problem solved! I actually called ListView.setAdapter(adapter) before the thumbnail downloading finished. So I put the ListView.setAdapter(adapter) in onPostExecute() and it works just fine

crystalWing
  • 69
  • 1
  • 11