2013-12-18 70 views
0

我有一個函數可以從資源中檢索圖像並將其顯示在GridView中。 Everithing工作正常,但由於性能問題,我想在運行時拇指創造,所以我創建一個新的ProgressDialog讓知道用戶的應用程序工作:Android - 線程返回值

import java.util.ArrayList; 

import android.app.Fragment; 
import android.app.ProgressDialog; 
import android.content.res.TypedArray; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.view.InflateException; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.GridView; 

import com.italiandevteam.chuck.adapter.GridViewAdapter; 
import com.italiandevteam.chuck.model.ImageItem; 

public class GalleriaPersonaggio extends Fragment{ 

    Integer personaggio = null; 
    private ProgressDialog progressDialog; 
    TypedArray imgs = null; 

    final ArrayList imageItems = new ArrayList(); 

    public GalleriaPersonaggio(int personaggio){ 

     this.personaggio = personaggio; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     int id = getIdPersonaggio(); 

     View rootView = null; 

     try { 
       rootView = inflater.inflate(R.layout.gallery_grid, container, false); 
      } 
     catch (InflateException e) { 
     } 

     final GridView gridView = (GridView) rootView.findViewById(R.id.gridView); 
     GridViewAdapter customGridAdapter = new GridViewAdapter(getActivity(), R.layout.gallery_row, getData(personaggio)); 
     gridView.setAdapter(customGridAdapter); 

     gridView.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
//    HashMap<String, Object> hm = gridView.getAdapter().getPosition(position); 
// 
//     String imgPath = (String) hm.get("flag"); //get downloaded image path 
//    Intent i = new Intent(getActivity(), MostraImmagine.class); //start new Intent to another Activity. 
//    i.putExtra("ClickedImagePath", imgPath); //put image link in intent. 
//    startActivity(i); 
      } 

    }); 

     return rootView; 
    } 

    public int getIdPersonaggio(){ 
     return this.personaggio; 
    } 

    private ArrayList getData(int personaggio) { 

     // retrieve String drawable array 

     switch(personaggio) 
     {   
      case 1:{ 
       imgs = getResources().obtainTypedArray(R.array.chuck_ids); 
       break; 
      } 
      case 2:{ 
       imgs = getResources().obtainTypedArray(R.array.sarah_ids); 
       break; 
      } 

      default:{ 
       imgs = getResources().obtainTypedArray(R.array.chuck_ids); 
      } 

     } 



     final ProgressDialog ringProgressDialog = ProgressDialog.show(getActivity(), "Please wait ...", "Loading Images ...", true); 
       ringProgressDialog.setCancelable(true); 
        new Thread(new Runnable() { 
         @Override 
         public void run() { 
          try { 

           int THUMBNAIL_HEIGHT = 100; 
           int THUMBNAIL_WIGHT = 100; 
           for (int i = 0; i < imgs.length(); i++) 
           { 
            Bitmap bitmap = BitmapFactory.decodeResource(getActivity().getResources(), imgs.getResourceId(i, -1)); 
            bitmap = Bitmap.createScaledBitmap(bitmap, THUMBNAIL_HEIGHT, THUMBNAIL_WIGHT, false); 
//         ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
//         bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
//         byte[] imageData = baos.toByteArray(); 
//         InputStream is = new ByteArrayInputStream(imageData); 
//         Bitmap b = BitmapFactory.decodeStream(is); 
            imageItems.add(new ImageItem(bitmap, "Image#" + i)); 
           } 

          } catch (Exception e) { 

          } 
          ringProgressDialog.dismiss(); 
         } 

        }).start(); 


     return imageItems; 

    } 
} 

的問題是,在最後的代碼不會返回任何內容,GridView是空的,如果我刪除它的效果很好的線程。

+0

您需要創建一個Handler來告訴UI線程在您檢索數據後更新GridView。爲了更容易,Android提供了'AsyncTask':http://developer.android.com/reference/android/os/AsyncTask.html – 323go

+0

如何將我的ArrayList傳遞給AsyncTask? –

+0

@ChristianGiupponi如果你仍然需要幫助,我做了一個AsyncTask的簡單例子。 – JJ86

回答

0

線程實例無法返回值,因爲run方法的返回類型爲void。你可以做的是實現delegate模式(Android中的監聽器),或切換到Callable接口和Executors。請注意,等待退貨類型可能沒有意義。如果你的東西:

僞代碼 UIThread:

variable = threadInstance.getResult(); 

你做一個異步調用,同步

0

正如指出的323go,您可以使用的AsyncTask(好得多恕我直言)創建一個內部類是這樣的:

private class MyAsyncTask extends AsyncTask<ArrayList<String>, Integer, Long> { 
    ... 
} 

整數用於更新進度對話框(如果你不需要一個,只是把太虛代替); Long是DoInBackground方法的結果。啓動您的AsyncTask是這樣的:

new MyAsyncTask().execute(passing); 

現在我不知道你的目標是什麼,但如果你遵循323go或this tutorial建議的鏈接,就可以提高你的代碼。

編輯

323go意見後,最好是傳遞的AsyncTask構造你的對象的ArrayList:

new MyAsyncTask(passing).execute(); 

當然,在你的AsyncTask類,你必須創建一個內部變量:

... 
private ArrayList<Object> myArrayList; 

private MyAsyncTask(ArrayList<Object> anArrayList) { 
    myArrayList = anArrayList; 
} 
... 
+1

將'ArrayList'傳遞給子類的構造函數並讓'doInBackground()'返回一個新的'ArrayList'在'onPostExecute()'中分配。這避免了併發問題,因爲'doInBackground()'運行在不同的威脅上。 – 323go

+0

@ 323go事實上,你是對的!謝謝 ;-) 。 – JJ86