Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8191231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:52:22+00:00 2026-06-07T03:52:22+00:00

i am trying to display image using GridView. This is the first time i

  • 0

i am trying to display image using GridView. This is the first time i using GridView, so i using the example from here and implement it to mine (i have tried the example that contained there, and it’s works).

But, i have checked it many time, there’s no error comes from LogCat, no Force Closed, the image didn’t show. i have no idea where’s the wrong part.

Here’s my code:

choosepic.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="@drawable/bg_inner">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/book_inner"
            android:layout_marginTop="50dp"
        />

        <ImageButton
            android:id="@+id/homeBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/home_btn"
            android:background="@null"
        />

        <ImageView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:src="@drawable/bg_arrow_btn"
            android:layout_alignParentRight="true"    
        />

        <ImageButton
            android:id="@+id/nextBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/right_arrow"
            android:background="@null"
            android:layout_alignParentRight="true"
            android:layout_marginTop="5dp"
            android:layout_marginRight="7dp"
            android:layout_marginLeft="7dp"
        />

        <ImageButton
            android:id="@+id/prevBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/left_arrow"
            android:background="@null"
            android:layout_toLeftOf="@+id/nextBtn"
            android:layout_marginTop="5dp"
        />

        <GridView
            android:id="@+id/gridView1"
            android:numColumns="3"
            android:gravity="center"
            android:columnWidth="30dp"
            android:stretchMode="columnWidth"
            android:layout_width="300dp"
            android:layout_height="200dp"
            android:layout_marginLeft="60dp"
            android:layout_marginTop="70dp"
        >

        </GridView>
</RelativeLayout>

animalbutton.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/grid_item_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true">
</ImageView>
<TextView 
    android:text="TextView" 
    android:layout_height="wrap_content"
    android:id="@+id/textView1" 
    android:layout_width="wrap_content"
    android:layout_centerHorizontal="true"
    android:textSize="18sp"
    android:visibility="gone">      
</TextView>

ImageAdapter.java

        public class ImageAdapter extends BaseAdapter{

    private Activity activity;
    private ArrayList<String> listNm;
    private ArrayList<Integer> listAnim;

    public ImageAdapter(Activity activity,ArrayList<String> listName, ArrayList<Integer> listImage) {
        super();
        this.listNm = listName;
        this.listAnim = listImage;
        this.activity = activity;
    }

    public static class ViewHolder
    {
        public ImageView imgViewAnim;
        public TextView txtViewAnim;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();

        if(convertView==null)
        {
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.animalbutton, null);

            view.txtViewAnim = (TextView) convertView.findViewById(R.id.textView1);
            view.imgViewAnim = (ImageView) convertView.findViewById(R.id.grid_item_image);

            convertView.setTag(view);
        }
        else
        {
            view = (ViewHolder) convertView.getTag();
        }

        view.txtViewAnim.setText(listNm.get(position));
        view.imgViewAnim.setImageResource(listAnim.get(position));

        return convertView;
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return listNm.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
}

choosepic.java

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

    ImageAdapter mAdapter;
    GridView gridView;
    static final String[] animal = new String[] { 
        "cat", "cow","croc", "duck", "elephant", "giraffe", "lion", "moose", "mouse"};

    private ArrayList<String> listNm;
    private ArrayList<Integer> listAnim;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choosepic);


        gridView = (GridView) findViewById(R.id.gridView1);
        prepare_list1();
        mAdapter = new ImageAdapter(this,listNm, listAnim);
        gridView.setAdapter(mAdapter);

        gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
                Toast.makeText(getApplicationContext(), mAdapter.getItem(position), Toast.LENGTH_SHORT).show();

            }
        });
    }

    public void prepare_list1(){
        listNm = new ArrayList<String>();
        listAnim = new ArrayList<Integer>();
        for (int i = 0; i < animal.length; i++) {
            listNm.add(animal[i]);
            listAnim.add(getResources().getIdentifier("anim_"+animal[i], "drawable", getPackageName()));

        }
    }
}

i need some help. i appreciate any help. thank you in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:52:23+00:00Added an answer on June 7, 2026 at 3:52 am

    i think the problem is in your getCount() that returns 0 elements
    instead of that make it like this

     @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return listNm.size();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to display image from database using php but i am
I'm trying to display image data read in from a binary file (I have
I'm trying to display an image using OpenCV. I have the following very basic
I am using this code to display image from URL, but it is not
I trying to display image in picture box. The application have two part. First
i am trying to display an image from net..and i am using ignition library
I am trying to use this answer from an old post: Display image from
I am trying to display an image from the /webroot/img/ folder using the following
I'm trying to display an image file from a directory using a PHP echo
I am trying to display an image using purely javascript. I have found various

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.