How do I dynamically populate the ImageView and TextView on my GridView/ListView Adapter?

I am working on my first app, and I am stuck on something. There are a lot of examples out there but none are doing exactly what I need to do and I lack the knowledge base to be able to adapt what I have found.

I am trying to create a GridView/ListView that displays an ImageView and TextView in the same grid/list row and the image and text are being dynamically updated on the row during scroll. There is a different image and text for every grid/row in the list (these are populated from an array) and I want to make sure I am re-using rows correctly during scrolling to keep the app's memory low. The images are stored in the Res.Drawable folder. An example of what I am trying to do would be the main screen of the WOW Armory application.

I have been looking at the following example and believe it is the answer, but I don’t know how to implement it:

The first problem I am having is handling the ImageViews in the Adapter and the next part would be setting up the MainActivity after the Adapter is working. Any help would be much appreciated and thank you in advance. Here is the code I have so far:


import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MainButtonAdapter extends BaseAdapter {
    private static ArrayList<MainButtonObject>arraylist;
    private LayoutInflater mInflater;

    public MainButtonAdapter(Context context, ArrayList<MainButtonObject>input) {        
        arraylist = input;
        mInflater = LayoutInflater.from(context);

    public int getCount() {        
        return arraylist.size();    

    public Object getItem(int position) {        
        return arraylist.get(position);    

    public long getItemId(int position) {        
        return position;    

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {        
        ViewHolder holder;        
        if (convertView == null) {  
            convertView = mInflater.inflate(R.layout.main_button_layout, null);
            holder = new ViewHolder();
            holder.icon = (ImageView)convertView.findViewById(;
            holder.text = (TextView)convertView.findViewById(;

        else {
            holder = (ViewHolder)convertView.getTag();
        return convertView;
    static class ViewHolder {
        ImageView icon;
        TextView text;


public class MainButtonObject {
    private String icon;
    private String icontxt;

    public MainButtonObject(String icon, String icontxt) {
        this.icon = icon;
        this.icontxt = icontxt;

    public String getIcon() {
    return icon;

    public void setIcon(String icon) {
        this.icon = icon;

    public String getIcontxt() {
        return icontxt;

    public void setIcontxt(String icontxt) {
        this.icontxt = icontxt;


<?xml version="1.0" encoding="utf-8"?>
    <string-array name="item1">
        <item>item 1</item>
    <string-array name="item2">
        <item>item 2</item>
    <string-array name="item3">
        <item>item 3</item>
    <string-array name="item4">
        <item>item 4</item>

Standard layout for the views:

<?xml version="1.0" encoding="utf-8"?>
    android:background="@drawable/background_main_button" >


And Finally the the MainActivity:

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

public void onCreate(Bundle savedInstanceState) {    

    GridView gridview = (GridView) findViewById(;    
    gridview.setAdapter(new MainButtonAdapter(this));    

    gridview.setOnItemClickListener(new OnItemClickListener() {        
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {            
            Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();        


From your source, it seems as though you have multiple arrays, each one mapping an image to some text. And what you want is basically a grid view to display the image and text on the same row? A grid view may not be the best choice, a list view is probably a better choice. A list view with a custom row that contains text and an image view.

In your code, I don't see the array actually being used unless you've left that out on purpose. Also, you've initialized your adapter like this...

new MainButtonAdapter(this)

but your adapter takes in a Context and an ArrayList...

public MainButtonAdapter(Context context, ArrayList<MainButtonObject>input)

So to start with, I would get your string arrays into objects...

String[] item1 = getResources().getStringArray(R.array.item1);
String[] item2 = getResources().getStringArray(R.array.item2);
String[] item3 = getResources().getStringArray(R.array.item3);
String[] item4 = getResources().getStringArray(R.array.item4);

And then create a MainButtonObject from those arrays...

MainButtonObject mbo1 = new MainButtonObject(item1[0],item1[1]);
MainButtonObject mbo2 = new MainButtonObject(item2[0],item2[1]);
MainButtonObject mbo3 = new MainButtonObject(item3[0],item3[1]);
MainButtonObject mbo4 = new MainButtonObject(item4[0],item4[1]);

And then add all of those to an ArrayList...

ArrayList<MainButtonObject> list = new ArrayList<MainButtonObject>();

And then create/set your adapter

gridview.setAdapter(new MainButtonAdapter(this,list));

In the adapter, I noticed you have...


Which is a String and is not valid in this case since setImageResource() takes in an integer that is the ID of the resource you want to access:

So the icons need to be in your res/drawable directory and then you can access them via, R.drawable.icon1 and so on.

I don't know if this is the answer you're looking for but I do hope it helps :)

Need Your Help

how to store selected items in an array from listview?

android listview imageview

how to put selected items from a custom listview with an imageview and textview into an array? Actually my imageview changes when selected.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.