Android 使用GridView定制适配器

Android 使用GridView定制适配器,android,android-gridview,Android,Android Gridview,我们是否可以在Gridview中设置图像,而无需制作自定义适配器。我的意思是我们可以直接用GridView设置预定义的ArrayAdapter。? 像下面的代码 GridViewGridView\u对象 ArrayAdapter<String> adapter=new ArrayAdapter<String>(context,into,int[]); gridview_object.setadapter(adapter); ArrayAdapter adapter=n

我们是否可以在
Gridview
中设置图像,而无需制作自定义适配器。我的意思是我们可以直接用
GridView
设置预定义的
ArrayAdapter
。? 像下面的代码

GridView
GridView\u对象

ArrayAdapter<String> adapter=new ArrayAdapter<String>(context,into,int[]);

gridview_object.setadapter(adapter);
ArrayAdapter adapter=newarrayadapter(context,into,int[]);
gridview_object.setadapter(适配器);

像这样的事。。。它会工作吗?

如果您试图使用自定义布局,则否。


如果需要自定义布局,则必须使用自定义适配器,将自定义布局的每个组件XML文件绑定到网格/回收器/列表的每个值的膨胀中的变量(
onCreate()
方法或
onCreateViewHolder()
RecyclerView
).

我不这么认为,但是实现一个图像适配器非常简单,从关于
GridView的文档中可以看到一个图像适配器的实现,请查看。

我不能完全同意前面的答案,因为我相信我们可以创建一个GridView(带图像和文本)无需使用自定义适配器。这有点棘手,但仍然很有可能。看看这个例子

   // Array of strings storing titles
    String[] titles = new String[] {
        "title1",
        "title2"
    };

    // Array of integers points to images stored in /res/drawable-ldpi/
    int[] icons = new int[]{
        R.drawable.icon1,
        R.drawable.icon2
    };

    //bind the icons & titles array inside a loop using HashMap so that
    // we can refer the keys & values in a single array for adapter
            List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

            for(int i=0;i<2;i++){
                HashMap<String, String> hm = new HashMap<String,String>();
                hm.put("title", titles[i]);
                hm.put("icon", Integer.toString(icons[i]) );
                aList.add(hm);
            }

             // refer the stored key & value of hashmap inside a single array 

            // Keys used in Hashmap
            String[] from = { "icon","title"};
            // Ids of views in gridviewview_layout
            int[] to = { R.id.icon,R.id.title};

            // Instantiating an adapter to store each items
            // R.layout.gridview_layout defines the layout of each item
            // set the single array contains the icons & titles in SimpleAdapter
           // 'from' refers the keys & 'to' refers the ids where the data will be displayed
            SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.gridview_layout, from, to);

            // Getting a reference to gridview of MainActivity
            GridView gridView = (GridView) findViewById(R.id.gridview);

            // Setting an adapter containing images to the gridview
            gridView.setAdapter(adapter);
gridview\u布局
layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    android:layout_gravity="center"
>

    <ImageView
        android:id="@+id/icon"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
    />

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        android:gravity="center_horizontal"
    />

</LinearLayout>

,这可以通过使用
SimpleAdapter
查看
simpledapter
的源代码

公共构造函数是

public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
            int resource, String[] from, int[] to)
您将看到该方法从每个
id(to)
检查找到的视图是否为
checkeable
TextView
ImageView
的实例,并设置相应映射值的值

基本上是台词-

                   if (v instanceof Checkable) {
                        if (data instanceof Boolean) {
                            ((Checkable) v).setChecked((Boolean) data);
                        } else if (v instanceof TextView) {
                            // Note: keep the instanceof TextView check at the bottom of these
                            // ifs since a lot of views are TextViews (e.g. CheckBoxes).
                            setViewText((TextView) v, text);
                        } else {
                            throw new IllegalStateException(v.getClass().getName() +
                                    " should be bound to a Boolean, not a " +
                                    (data == null ? "<unknown type>" : data.getClass()));
                        }
                    } else if (v instanceof TextView) {
                        // Note: keep the instanceof TextView check at the bottom of these
                        // ifs since a lot of views are TextViews (e.g. CheckBoxes).
                        setViewText((TextView) v, text);
                    } else if (v instanceof ImageView) {
                        if (data instanceof Integer) {
                            setViewImage((ImageView) v, (Integer) data);                            
                        } else {
                            setViewImage((ImageView) v, text);
                        }
                    } else {
                        throw new IllegalStateException(v.getClass().getName() + " is not a " +
                                " view that can be bounds by this SimpleAdapter");
                    }
if(v instanceof checkeble){
if(布尔值的数据实例){
(可检查的)v.setChecked((布尔)数据);
}else if(文本视图的v实例){
//注意:将文本视图检查的实例保留在这些选项的底部
//ifs,因为很多视图都是文本视图(例如复选框)。
setViewText((TextView)v,text);
}否则{
抛出新的IllegalStateException(v.getClass().getName())+
应绑定到布尔值,而不是+
(data==null?“:data.getClass());
}
}else if(文本视图的v实例){
//注意:将文本视图检查的实例保留在这些选项的底部
//ifs,因为很多视图都是文本视图(例如复选框)。
setViewText((TextView)v,text);
}else if(图像视图的v实例){
if(整数的数据实例){
setViewImage((ImageView)v,(整数)数据);
}否则{
setViewImage((ImageView)v,文本);
}
}否则{
抛出新的IllegalStateException(v.getClass()。getName()+“不是”+
“可受此SimpleAdapter约束的视图”);
}

是的,我同意你的观点,如果我想要自定义布局,我必须使用自定义适配器。但是如果我不想要,任何预定义的适配器都可以将图像设置到网格视图中,而不扩展基本适配器吗?我明白了,但是:没有一个预定义的适配器可以接受图像集合作为参数。您必须自己创建一个。如果您使用自定义布局,则不必使用自定义适配器。这不是真的。您可以将自己的布局与任何适配器一起使用,该适配器的构造函数将viewId与资源(布局)Id一起使用。请详细说明您的编码“List aList=new ArrayList();for(int i=0;iHm.)的这一部分。我不知道这一点!
private void bindView(int position, View view) 
                   if (v instanceof Checkable) {
                        if (data instanceof Boolean) {
                            ((Checkable) v).setChecked((Boolean) data);
                        } else if (v instanceof TextView) {
                            // Note: keep the instanceof TextView check at the bottom of these
                            // ifs since a lot of views are TextViews (e.g. CheckBoxes).
                            setViewText((TextView) v, text);
                        } else {
                            throw new IllegalStateException(v.getClass().getName() +
                                    " should be bound to a Boolean, not a " +
                                    (data == null ? "<unknown type>" : data.getClass()));
                        }
                    } else if (v instanceof TextView) {
                        // Note: keep the instanceof TextView check at the bottom of these
                        // ifs since a lot of views are TextViews (e.g. CheckBoxes).
                        setViewText((TextView) v, text);
                    } else if (v instanceof ImageView) {
                        if (data instanceof Integer) {
                            setViewImage((ImageView) v, (Integer) data);                            
                        } else {
                            setViewImage((ImageView) v, text);
                        }
                    } else {
                        throw new IllegalStateException(v.getClass().getName() + " is not a " +
                                " view that can be bounds by this SimpleAdapter");
                    }