Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - return value from thread
    primarykey
    data
    text
    <p>I have a function that retrieve images from the resource and display it in a <code>GridView</code>. Everithing works fine but due to a performance issue I would like to create at runtime thumbs so I create a new <code>ProgressDialog</code> to let know the user that the app is working:</p> <pre><code>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&lt;?&gt; parent, View v, int position, long id) { // HashMap&lt;String, Object&gt; 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 &lt; 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; } } </code></pre> <p>The problem is that at the end the code doesn't return anything, the <code>GridView</code> is empty, if I remove the thread it works well.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload