从其他线程更改视图(Change View from other thread)

2019-07-29 01:33发布

我写了一个代码,从互联网上下载的图像。 我必须表明它在一个ImageView被动态创建。

而我得到的是只有原来的线程创建视图层次可以触摸其观点错误。 我知道我必须写一个手柄,但我怎么能做到这一点?

这里是我的代码:

public class ResimCek implements Runnable {

        int resimID = 0;

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

        public void 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); // *************************
        }
    }

    private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
        try {
            InputStream is = (InputStream) this.fetch(url);
            Drawable d = Drawable.createFromStream(new URL(url).openConnection().getInputStream(),saveFilename);
            return d;
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public Object fetch(String address) throws MalformedURLException,IOException {
        URL url = new URL(address);
        Object content = url.getContent();
        return content;
    }
    private void MalzemeEkle(String malzemeKodu, String malzemeTanimi) {
        ImageView parcaresmi = new ImageView(this);
        parcaresmi.setId(Integer.parseInt(malzemeKodu));
        Runnable r = new ResimCek(Integer.parseInt(malzemeKodu)); 
        new Thread(r).start(); 
}

Answer 1:

public class ResimCek implements Runnable {

        int resimID = 0;

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

        public void 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(); 

通过任何情况下,如果这是一个活动..然后

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

也将工作..



Answer 2:

您应该创建一个处理程序:

    final Handler   myHandler   = new Handler() {
                                    @Override
                                    public void handleMessage(Message msg)
                                    {
                                        /*do all your ui action here to display the image ()*/
                                        resim.setImageDrawable(image);
                                    }
                                };

而在你的可运行当图像被下载调用:

myHandler.sendEmptyMessage(0);

还有其他的选择处理程序进行处理,你可以在这里找到http://developer.android.com/reference/android/os/Handler.html



文章来源: Change View from other thread