سؤال

Okay, so I have a JSON array of image URLs. I want to load them into a horizontal scrolling view. This is the way I've gone about it, but nothing gets displayed where the ScrollView should go:

public void run() {
    JSONArray photosArray;
    caption = new WebView(thisContext);
    imageScroller = new HorizontalScrollView(thisContext);
    imagesHolder = new LinearLayout(thisContext);
    imagesHolder.setOrientation(LinearLayout.HORIZONTAL);
    try {
        photosArray = new JSONArray(postData.getString("photos"));
        for(int i = 0; i < photosArray.length(); i++) {
            WebView iV = new WebView(thisContext);
            JSONObject thisPhoto = photosArray.getJSONObject(i);
            JSONArray sizesArray = new JSONArray(thisPhoto.getString("alt_sizes"));
            JSONObject largest = sizesArray.getJSONObject(0);
            iV.loadData("<img src=\""+largest.getString("url")+"\" />", "text/html", null);
            imagesHolder.addView(iV);
        }
        imageScroller.addView(imagesHolder);
        myPostHolder.addView(imageScroller);
        caption.loadData(postData.getString("caption"),"text/html",null);
        myPostHolder.addView(caption);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Note that this is done in a runnable class. Thanks a lot

هل كانت مفيدة؟

المحلول

There are two rules in the Android single thread model.

  1. Do not block the UI thread
  2. Do not access the Android UI toolkit from outside the UI thread

So I don't think you can add views to a viewgroup outside of the main thread. Please see: http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html for a fuller explanation.

Also, you need to make sure you are actually setting the contentview and you may need to call invalidate() on the viewgroup to cause a redraw.

And, you probably should be using a GridView..can't see why you'd use a webview for this.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top