Custom Adapters with GridView

Is it possible to set images in Gridviewwithout creating a CustomAdapter . I mean, can we directly set the predefined ArrayAdapterusing Gridview.? Like the following code

Gridview gridview_object;

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

gridview_object.setadapter(adapter);

Something like this ... Will it work?

+4
source share
4 answers

I don’t think so, but it’s very simple to implement ImageAdapter , this page from about documents GridViewcontains an implementation of ImageAdapter , check it out.

+2
source

, , 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);

activity_main , GridView

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/gridview"
    android:numColumns="auto_fit"
/>

gridview_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>
+2

, .

, , XML // (onCreate() onCreateViewHolder() RecyclerView).

+1

. SimpleAdapter SimpleAdapter .

public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
            int resource, String[] from, int[] to)

Here, the last parameter - is an array of identifiers that you provide.

Now let's look at the method

private void bindView(int position, View view) 

You will see that the method checks the found view from each ids(to), whether it is an instance Checkable, TextViewor ImageViewand sets the value of the corresponding displayed values.

Mostly lines -

                   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");
                    }
0
source

Source: https://habr.com/ru/post/1599359/


All Articles