我有一個函數可以從資源中檢索圖像並將其顯示在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
是空的,如果我刪除它的效果很好的線程。
您需要創建一個Handler來告訴UI線程在您檢索數據後更新GridView。爲了更容易,Android提供了'AsyncTask':http://developer.android.com/reference/android/os/AsyncTask.html – 323go
如何將我的ArrayList傳遞給AsyncTask? –
@ChristianGiupponi如果你仍然需要幫助,我做了一個AsyncTask的簡單例子。 – JJ86