开发者

GridView with images from website and textView?

In my android app /i'm trying to do sometrhing like that http://i56.tinypic.com/2ivno8l.jpg. I found examples how to make a gridview with images or with textview here : http://www.bogotobogo.com/Android/android6ListViewSpinnerGridViewGalleryB.html#GridView but I don't know how to do with images and textview too. Can anyone help me?

Thanks..

I tried :

package com.MyGridView;

import java.io.InputStream;
import java.net.URI;
import java.net.URL;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

public class ImageAdapter1 extends BaseAdapter {

    private Context mContext;

 开发者_如何学C   private LayoutInflater inflater = null;

    private Activity activity;

    public ImageAdapter1(Context c) {
        mContext = c;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return filesnames.length;
    }

    public Object getItem(int position) {
        return null;
    }

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

    public class ViewHolder {
        public TextView text;
        public ImageView image;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        ViewHolder holder;
        if (convertView == null) {
            vi = inflater.inflate(R.layout.item, null);
            holder = new ViewHolder();
            holder.text = (TextView) vi.findViewById(R.id.text);
            holder.image = (ImageView) vi.findViewById(R.id.image);

            holder.image.setLayoutParams(new GridView.LayoutParams(85, 85));
            holder.image.setScaleType(ImageView.ScaleType.CENTER_CROP);
            holder.image.setPadding(8, 8, 8, 8);

        } else
            holder = (ViewHolder) vi.getTag();

        holder.text.setText(WORLDCUP2010[position]);
        holder.image.setImageResource(filesnames[position]);

        return vi;
    }


    private Integer[] filesnames = { R.drawable.icon, R.drawable.icon,
            R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon,
            R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon,
            R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon,
            R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon

    };

    static final String[] WORLDCUP2010 = new String[] { "Algeria", "Argentina",
            "Australia", "Brazil", "Cote d'Ivoire", "Cameroon", "Chile",
            "Costa Rica", "Denmark", "England", "France", "Germany", "Ghana",
            "Greece", "Honduras", "Italy", "Japan", "Netherlands",
            "New Zealand", "Nigeria", "North Korea", "Paraguay", "Portugal", };

}

and I get this in Logcat :

07-21 17:30:43.152: ERROR/AndroidRuntime(396): Uncaught handler: thread main exiting due to uncaught exception
07-21 17:30:43.172: ERROR/AndroidRuntime(396): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.MyGridView/com.MyGridView.MyGridView}: java.lang.NullPointerException
07-21 17:30:43.172: ERROR/AndroidRuntime(396):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
07-21 17:30:43.172: ERROR/AndroidRuntime(396):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
07-21 17:30:43.172: ERROR/AndroidRuntime(396):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
07-21 17:30:43.172: ERROR/AndroidRuntime(396):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
07-21 17:30:43.172: ERROR/AndroidRuntime(396):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-21 17:30:43.172: ERROR/AndroidRuntime(396):     at android.os.Looper.loop(Looper.java:123)
07-21 17:30:43.172: ERROR/AndroidRuntime(396):     at android.app.ActivityThread.main(ActivityThread.java:4363)
07-21 17:30:43.172: ERROR/AndroidRuntime(396):     at java.lang.reflect.Method.invokeNative(Native Method)
07-21 17:30:43.172: ERROR/AndroidRuntime(396):     at java.lang.reflect.Method.invoke(Method.java:521)
07-21 17:30:43.172: ERROR/AndroidRuntime(396):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-21 17:30:43.172: ERROR/AndroidRuntime(396):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-21 17:30:43.172: ERROR/AndroidRuntime(396):     at dalvik.system.NativeStart.main(Native Method)
07-21 17:30:43.172: ERROR/AndroidRuntime(396): Caused by: java.lang.NullPointerException
07-21 17:30:43.172: ERROR/AndroidRuntime(396):     at com.MyGridView.ImageAdapter1.<init>(ImageAdapter1.java:34)
07-21 17:30:43.172: ERROR/AndroidRuntime(396):     at com.MyGridView.MyGridView.onCreate(MyGridView.java:23)
07-21 17:30:43.172: ERROR/AndroidRuntime(396):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-21 17:30:43.172: ERROR/AndroidRuntime(396):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
07-21 17:30:43.172: ERROR/AndroidRuntime(396):     ... 11 more


Just google for examples about custom adapters including textview and imageviews.

I found an example here: http://www.stealthcopter.com/blog/2010/09/android-creating-a-custom-adapter-for-gridview-buttonadapter/

Edited:

One great example is efficientAdapter found here: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html In that example they use a layout inflater instead of creating the ImageViews (or buttons) from context.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜