4

Can we set images in Gridview without making a CustomAdapter. I mean can we directly set predefined ArrayAdapter with GridView.? Like the following Code

GridView gridview_object;

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

gridview_object.setadapter(adapter);

Something like that... Will it work?

N J
  • 27,217
  • 13
  • 76
  • 96
Jitender
  • 123
  • 1
  • 12

4 Answers4

2

I don't think so, but it's really simple to implement an ImageAdapter, this page from the docs about GridView contains an implementation of an ImageAdapter, check it out.

N J
  • 27,217
  • 13
  • 76
  • 96
Rodrigo Direito
  • 748
  • 6
  • 21
2

I can't completely agree with the previous answers as I believe we can create a GridView (with image and text) without using a custom adapter. It's a little bit tricky but still pretty much possible. See the example,

   // 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 layout containing the 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 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>
Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50
  • Please elaborate this part of your coding" List> aList = new ArrayList>(); for(int i=0;i<2;i++){ HashMap hm = new HashMap(); hm.put("title", titles[i]); hm.put("icon", Integer.toString(icons[i]) ); aList.add(hm); } // Keys used in Hashmap String[] from = { "icon","title"}; // Ids of views in listview_layout int[] to = { R.id.icon,R.id.title}; – Jitender Jul 23 '15 at 16:52
  • Hm.. didn't know about this! – VulfCompressor Jul 23 '15 at 17:31
1

If you are trying to use custom layouts, then No.

If you want a custom layout you must use custom adapters, binding every component of your custom layout XML file to a variable in the inflation of each value of your Grid/Recycler/List view (onCreate() method or onCreateViewHolder() with RecyclerView).

VulfCompressor
  • 1,390
  • 1
  • 11
  • 26
  • Yes I agree with you that if I want custom layout I must use custom adapter..But if I don't want that .Can any Predefine adapter sets the images into grid view without extending the base adapter ?? – Jitender Jul 23 '15 at 16:40
  • I see, but still: there isn't a pre-defined adapter which accepts an collection of images as parameters. You have to create one by yourself. – VulfCompressor Jul 23 '15 at 16:42
  • If you use a custom layout It is not must that you will use a custom adapter. Its not true. You can use your own layout with any adapter which constructor takes the viewId along with resource(layout) Id. – Sanjeet A Jul 23 '15 at 17:27
0

YES,This can be Achieved by using the SimpleAdapter Have a look on the source code of SimpleAdapter here.

The public constructor is

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

Here the last parameter to is the array of the ids you are providing.

Now have a look on the method

private void bindView(int position, View view) 

You will see the method check the found view from the each ids(to) whether it is instance of Checkable , TextView or ImageView and set the value of corresponding mapped valued.

Basically the 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");
                    }
Sanjeet A
  • 5,171
  • 3
  • 23
  • 40