带有来自网站和文本视图的图像的 GridView?
在我的android应用程序中/我正在尝试做一些类似的事情 http://i56.tinypic.com/2ivno8l .jpg。我在这里找到了如何使用图像或文本视图制作网格视图的示例: http://www. bogotobogo.com/Android/android6ListViewSpinnerGridViewGalleryB.html#GridView 但我也不知道如何处理图像和文本视图。谁能帮助我吗?
谢谢..
我尝试过:
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;
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", };
}
并且我在 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
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;
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需谷歌搜索有关自定义适配器(包括文本视图和图像视图)的示例即可。
我在这里找到了一个例子:
http://www. Stealthcopter.com/blog/2010/09/android-creating-a-custom-adapter-for-gridview-buttonadapter/
编辑:
一个很好的例子是efficientAdapter在这里找到: http:// developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html
在该示例中,他们使用布局充气器而不是从上下文创建 ImageView(或按钮)。
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.