Skip to content Skip to sidebar Skip to footer

Change View From Other Thread

I wrote a code to download an image from internet. And i have to show it in a ImageView which is dynamically created. And i am getting an error that Only the original thread that c

Solution 1:

publicclassResimCekimplementsRunnable{

        int resimID = 0;

        public ResimCek(int parcaID) {
            // store parameter for later user
            resimID = parcaID;
        }

        publicvoid run() {

            int resID = getResources().getIdentifier(Integer.toString(resimID) , "tag", getPackageName()); 
            ImageView resim = (ImageView) findViewById(resID);

            Drawable image = ImageOperations(getBaseContext(),"http://141.11.11.206/parca/" + Integer.toString(resimID) + ".jpg" ,"I" + Integer.toString(resimID) + ".jpg");

            // I AM GETTING ERROR HERE ******************
            resim.setImageDrawable(image); // *************************
        }
    }



new Handler().post(new ResimCek(Integer.parseInt(malzemeKodu))); instead of new Thread(r).start(); 

by any case if this is an Activity.. then

runOnUIThread(new ResimCek(Integer.parseInt(malzemeKodu))); `instead of new Thread(r).start();` 

will also work..

Solution 2:

You should create a handler :

finalHandlermyHandler=newHandler() {
                                    @OverridepublicvoidhandleMessage(Message msg)
                                    {
                                        /*do all your ui action here to display the image ()*/
                                        resim.setImageDrawable(image);
                                    }
                                };

And in your runnable when the image is downloaded call :

myHandler.sendEmptyMessage(0);

There are other options for handler you can find here http://developer.android.com/reference/android/os/Handler.html

Post a Comment for "Change View From Other Thread"