我有类名 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;
}
}