Reputation: 123
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?
Upvotes: 4
Views: 823
Reputation: 1410
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
).
Upvotes: 1
Reputation: 11873
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>
Upvotes: 2
Reputation: 5227
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");
}
Upvotes: 0
Reputation: 746
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.
Upvotes: 2