0

我有类名 Main1,它从主类获取图像链接数组和文本数组并将它们显示在listview. 我收到错误“只有创建视图层次结构的原始线程才能触及它的视图”所以我搜索我发现我使用的解决方案,runOnUithread但它只能用于,oncreate所以我不知道这是解决方案。这是从 main1 类中获取的

main1 类是

public class Main1 extends ArrayAdapter<String> {

private final Activity context;
private final String[] Cat_Name;
private final String[] logo;
public Main1(Activity context,String[] Cat_Name, String[] logo) {
super(context, R.layout.main1, Cat_Name);
this.context = context;
this.Cat_Name = Cat_Name;
this.logo = logo;
}
@Override

public View getView(final int position, View convertView, ViewGroup parent) {





    LayoutInflater inflater = context.getLayoutInflater();
    View rowView= inflater.inflate(R.layout.main1, null, true);
    TextView txtTitle = (TextView) rowView.findViewById(R.id.textView1);


    final ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
    txtTitle.setText(Cat_Name[position]);


    Thread thread = new Thread() {
        @Override
        public void run() {
            try {


                                    String img_url=logo[position]; //url of the image
        //      System.out.println(img_url);
                URL url = new URL(img_url);
                final Bitmap bmp; 
                bmp=BitmapFactory.decodeStream(url.openConnection().getInputStream());
                        imageView.setImageBitmap(bmp);


            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }
    };


    thread.start();



    return rowView;

}



}
4

1 回答 1

0

在 runOnUiThread 方法中设置您的图像,如下所示

  ((Activity) context).runOnUiThread(new Runnable() {
        @Override
        public void run() {
            imageView.setImageBitmap(bmp);
        }
    });

希望有帮助。

于 2015-08-10T18:36:52.673 回答