Hi there,

Please take a look at:
http://kombadil.willflow.com/misbehaving_grid/

The grid starts out OK, but if you move around, the alignment is
messed up. Any suggestions?

Thanks!

Ken

P.S. I've attached the code below:

----- main.xml -----
<?xml version="1.0" encoding="utf-8"?>

<GridView xmlns:android="http://schemas.android.com/apk/res/android";
        android:id="@+id/launch_grid"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    android:padding="10dip"
    android:verticalSpacing="10"
    android:horizontalSpacing="10"
    android:numColumns="auto_fit"
    android:columnWidth="60"
    android:stretchMode="columnWidth"

    android:gravity="center"
/>

----- ThreadTest.java -----
package com.test.threadtest;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ThreadTest extends Activity {
        private List<Icon> icons;
        private GridView launchView;
        private IconListAdapter iconListAdapter;

        @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        final Handler mHandler = new Handler();
        icons = new ArrayList<Icon>();

        Thread t = new Thread() {
                        public void run() {
                                perform();
                                mHandler.post(mUpdate);
                        }
                };

                t.start();
    }

    protected void perform() {
        ServerHandler serverHandler = new ServerHandler();
        icons = serverHandler.getIcons();
        iconListAdapter = new IconListAdapter(this, icons);

        }

    final Runnable mUpdate = new Runnable() {
        public void run() {
                launchView = (GridView) findViewById(R.id.launch_grid);
                launchView.setAdapter(iconListAdapter);
                launchView.setOnItemClickListener(new OnItemClickListener() {
                        public void onItemClick(AdapterView parent, View v, int
position, long id) {
                                Intent i = new Intent(ThreadTest.this, 
ThreadTest.class);
                                startActivity(i);
                        }
                });


        }
    };

    public class IconListAdapter extends BaseAdapter {
        private Context ctx;
        private List<Icon> icons;

                public IconListAdapter(Context ctx, List<Icon> icons) {
                        this.ctx = ctx;
                        this.icons = icons;
        }

        public View getView(int position, View convertView, ViewGroup
parent) {
                Icon app = icons.get(position);
                ApplicationView av = new ApplicationView(ctx, app.name,
app.iconUrl);
                return av;
        }

        public final int getCount() {
            return icons.size();
        }

        public final Object getItem(int position) {
            return icons.get(position);
        }

        public final long getItemId(int position) {
            return position;
        }
    }

    public static Bitmap getBitmap(String urlString) {
                Bitmap bm = null;

                if (urlString.startsWith("http")) {
                        try {
                                URL url = new URL(urlString);
                                HttpURLConnection conn = (HttpURLConnection)
url.openConnection();
                                conn.connect();
                                InputStream is = conn.getInputStream();
                                BufferedInputStream bis = new 
BufferedInputStream(is);
                                bm = BitmapFactory.decodeStream(bis);
                                conn.disconnect();
                                is.close();
                                bis.close();
                        } catch (Exception e) {
                                Log.e("error", e.toString());
                                bm = null;
                        }
                }

                return bm;
        }

        public class ApplicationView extends LinearLayout {
                private TextView name;
                private ImageView icon;

                public ApplicationView(Context context, String name, String
urlString) {
                        super(context);
                        this.setOrientation(VERTICAL);

                        this.icon = new ImageView(context);
                        addView(icon, new
LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
                        this.icon.setImageBitmap(getBitmap(urlString));

                this.name = new TextView(context);
                        addView(this.name, new
LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
                        this.name.setText(name);
                }
        }
}


----- ServerHandler.java -----
package com.test.threadtest;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;

public class ServerHandler {
        public List<Icon> getIcons() {
                final List<Icon> icons = new ArrayList<Icon>();

                String name = "icon";

                for(int i=0; i<10; i++) {
                        Icon icon = new Icon();
                        icon.name = name;
                        name += "xxx";
                        icon.iconUrl = "http://code.google.com/android/images/
logo_android.gif";
                        icons.add(icon);
                }

                // Sleep code
                // Comment this section out and the code would work.
                // In reality it is a piece of code that talks to a server.
//              try {
//                      Log.e("test", "enter");
//                      Thread.sleep(1000);
//                      Log.e("test", "leave");
//              } catch (InterruptedException e) {
//                      e.printStackTrace();
//              }

                return icons;
        }
}



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new M5 SDK!
http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to