Problem with Gallery and remote images

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Problem with Gallery and remote images

Postby tcell » Wed Apr 22, 2009 3:01 am

I'm writing an app that calls a PHP server to, among other things, get a list of image URL's which I'd like to load into a Gallery widget for one of the Activities.

In the log, when the Activity loads, I can see the Gallery being inflated, the ImageAdapter being set and constructed, but it never calls the getView method of the ImageAdapter. I do see it calling the getCount method twice, but it never tries to load the images. I can verify this both on the Android side (logging statements in the image loading code are never logged) and on the PHP side (the server never gets requests for the images).

The code is based on http://www.androidsnippets.org/snippets/25/

Here's the XML in the layout file to define the gallery:


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <Gallery android:id="@+id/gallery"
  3.  
  4.          android:layout_width="fill_parent"
  5.  
  6.          android:layout_height="wrap_content"
  7.  
  8.          android:gravity="bottom"
  9.  
  10. />
  11.  
  12.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Here is the code for my Activity's onCreate() method:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         public void onCreate(Bundle savedInstanceState) {
  3.  
  4.                 super.onCreate(savedInstanceState);
  5.  
  6.                 setContentView(R.layout.gallery_view);
  7.  
  8.                
  9.  
  10.                 Gallery gallery = ((Gallery) findViewById(R.id.gallery));
  11.  
  12.                 Log.e("GAL", "found gallery");
  13.  
  14.                
  15.  
  16.                 ImageAdapter theAdapter  = new ImageAdapter(this);
  17.  
  18.                
  19.  
  20.                 gallery.setAdapter(theAdapter);
  21.  
  22.                
  23.  
  24.                 Log.e("GAL", "set gallery adapter");
  25.  
  26.  
  27.  
  28.                 Button searchButton = (Button)findViewById(R.id.search_button);
  29.  
  30.         searchButton.setOnClickListener(mSearchClickedHandler);
  31.  
  32.         Log.e("GALLERY", "in onCreate()");
  33.  
  34.         ImageView title_bar = (ImageView)findViewById(R.id.banner_image);
  35.  
  36.         title_bar.setOnClickListener(mTitleBarClickedHandler);
  37.  
  38.        
  39.  
  40.         Bundle extras = getIntent().getExtras();
  41.  
  42.                
  43.  
  44.                 String title = (String)extras.getString("title");
  45.  
  46.                
  47.  
  48.                 gallery_id = (String)extras.getString("id");
  49.  
  50.                
  51.  
  52.                 TextView title_text = (TextView)findViewById(R.id.title_text);
  53.  
  54.                 title_text.setText(title);
  55.  
  56.  
  57.  
  58.         }
  59.  
  60.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


And here's the inner class ImageAdapter:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.             public class ImageAdapter extends BaseAdapter {
  3.  
  4.  
  5.  
  6.                 /** The parent context */
  7.  
  8.  
  9.  
  10.                    private Context myContext;
  11.  
  12.                    private int count;
  13.  
  14.                    
  15.  
  16.  
  17.  
  18.  
  19.  
  20.                    /** Simple Constructor saving the 'parent' context. */
  21.  
  22.  
  23.  
  24.                    public ImageAdapter(Context c) { this.myContext = c;
  25.  
  26.                    
  27.  
  28.                    
  29.  
  30.                    Log.e("GAL", "constructed");
  31.  
  32.                    
  33.  
  34.                    }
  35.  
  36.  
  37.  
  38.                    /* Use the array-Positions as unique IDs */
  39.  
  40.  
  41.  
  42.                    public Object getItem(int position) { Log.e("GAL", "in getItem returning Object at position " + position);return position; }
  43.  
  44.  
  45.  
  46.                    public long getItemId(int position) { Log.e("GAL", "in getItem returning long at position " + position);return position; }
  47.  
  48.  
  49.  
  50.  
  51.  
  52.                    public View getView(int position, View convertView) {
  53.  
  54.  
  55.  
  56.                        ImageView i = new ImageView(this.myContext);
  57.  
  58.                        Log.e("GAL", "in getView with 2 args");
  59.  
  60.  
  61.  
  62.                        try {
  63.  
  64.    
  65.  
  66.    
  67.  
  68.                                                         String jsonUrl = getString(R.string.api_url) + "?action=galleryimages&id=" + GalleryView.this.gallery_id;
  69.  
  70.                                                         JSONHandler jsonHandler = new JSONHandler(jsonUrl);
  71.  
  72.                                                         JSONArray joe = new JSONArray(jsonHandler.getJson());
  73.  
  74.                                                         count = joe.length();
  75.  
  76.                                                                
  77.  
  78.                                                         JSONObject gallery_entry = joe.getJSONObject(position);
  79.  
  80.                                                         String img_content_id = gallery_entry.getString("img_content_id");
  81.  
  82.                                                        
  83.  
  84.                                                         String dam_url = getResources().getString(R.string.dam_url) + "/access/show/id/" + img_content_id + "/ds/master/width/120";
  85.  
  86.                                                         Log.e("DAM", dam_url);
  87.  
  88.                                                         ImageHandler im = new ImageHandler();
  89.  
  90.                                                                
  91.  
  92.                                                         i.setImageBitmap(im.getBitmap(dam_url));
  93.  
  94.                                                         i.setScaleType(ImageView.ScaleType.FIT_XY);
  95.  
  96.  
  97.  
  98.                         }
  99.  
  100.                        catch (Exception e) {
  101.  
  102.  
  103.  
  104.                            Log.e("GAL", "Remote Image Exception", e);
  105.  
  106.  
  107.  
  108.                        }
  109.  
  110.                    
  111.  
  112.                           /* Image should be scaled as width/height are set. */
  113.  
  114.  
  115.  
  116.                        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
  117.  
  118.  
  119.  
  120.                        /* Set the Width/Height of the ImageView. */
  121.  
  122.  
  123.  
  124.                        i.setLayoutParams(new Gallery.LayoutParams(150, 150));
  125.  
  126.  
  127.  
  128.                        return i;
  129.  
  130.  
  131.  
  132.                    }
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.                    /** Returns the size (0.0f to 1.0f) of the views
  141.  
  142.  
  143.  
  144.                     * depending on the 'offset' to the center. */
  145.  
  146.  
  147.  
  148.                    public float getScale(boolean focused, int offset) {
  149.  
  150.  
  151.  
  152.                      /* Formula: 1 / (2 ^ offset) */
  153.  
  154.                            Log.e("GAL", "in getScale");
  155.  
  156.                        return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
  157.  
  158.  
  159.  
  160.                    }
  161.  
  162.  
  163.  
  164.                         public int getCount() {
  165.  
  166.                                 // TODO Auto-generated method stub
  167.  
  168.                                 Log.e("GAL", "in getCount returning int");
  169.  
  170.                                 return count;
  171.  
  172.                         }
  173.  
  174.  
  175.  
  176.                         public View getView(int arg0, View arg1, ViewGroup arg2) {
  177.  
  178.                                 // TODO Auto-generated method stub
  179.  
  180.                                 Log.e("GAL", "in getView with 3 args");
  181.  
  182.                                 return getView(arg0, arg1);
  183.  
  184.                         }
  185.  
  186.  
  187.  
  188.                }
  189.  
  190.  
  191.  
  192.  
Parsed in 0.043 seconds, using GeSHi 1.0.8.4


I'd really be thankful for any help anyone can give me with this.

thanks,
Tom
tcell
Freshman
Freshman
 
Posts: 3
Joined: Fri Mar 27, 2009 12:37 am

Top

Postby padde » Wed Apr 22, 2009 11:13 am

Try to initiate count with zero. Not sure whether this helps.. but you said getCount gets called and in this
code it will return null because getView wastn called at that time. Maybe there is a check if count is zero
before the gallery gets filled with getView calls... dont know but checking this wont hurt :D

Greets
Padde
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

setting count variable to 0

Postby tcell » Wed Apr 22, 2009 7:21 pm

Thanks Padde - I tried that but got the same result. Also made sure count is getting set to the number of remote images that come back from the server.
tcell
Freshman
Freshman
 
Posts: 3
Joined: Fri Mar 27, 2009 12:37 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 3 guests