0

i have class name Main1 that get array of image links & array of text from the main class and display them on listview. i get error "only the original thread that created a view hierarchy can touch its views" so i search for the solution i found that i use runOnUithread but it can use only on the oncreateso i don't know that is the solution. the is is get from the main1 class

the main1 class is

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;

}



}
Rishabh Agrawal
  • 1,998
  • 2
  • 25
  • 40
Break Hart
  • 169
  • 1
  • 1
  • 7

1 Answers1

0

set your image in runOnUiThread method like below

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

Hope that helps.

Moubeen Farooq Khan
  • 2,875
  • 1
  • 11
  • 26