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

  • Home
  • SEARCH
  • 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 7991913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:25:39+00:00 2026-06-04T13:25:39+00:00

I had used a ListView to show some data (String) retrieved from the database…quite

  • 0

I had used a ListView to show some data (String) retrieved from the database…quite simple, but one of my cooworkers decided to put an image in front of the text in the screen. We were not able to do it with Listview, but we found some example with GridView that looked exactly as we need then we spent some time using GridView and did put the image in the front of the text.

Now, unfortunately we don’t know how to get the selected item (based on the Text that comes from the item selected)

I’m using OnItemClickListener, and within onItemClick the method gridView.getItemSelectedAtPosition(), but everything i get is “null”.

I don’t now if ImageAdapter has something to do with it, cuz if I change it to an ArrayAdapter I can get the right item properly but there is no image in the row in this way.

gridview.setOnItemClickListener(new OnItemClickListener() {  
   public void onItemClick(AdapterView<?> parent, View v, int position, long id{     String selectedItem = (String) (gridView.getItemAtPosicion(position)); //always null     } });

what can I do?

EDITED – ListView source

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 ImageAdapter extends BaseAdapter {
private Context context;
private final String[] mobileValues;
private final String[] mobileValuesD;

public ImageAdapter(Context context, String[] mobileValues, String[] mobileValuesD) {
this.context = context;
this.mobileValues = mobileValues;
this.mobileValuesD = mobileValuesD;
}

  public View getView(int position, View convertView, ViewGroup parent) { 
  LayoutInflater inflater = (LayoutInflater) context.getSystemService           (Context.LAYOUT_INFLATER_SERVICE);
  View gridView;

  if (convertView == null) {

  gridView = new View(context);

   // get layout from mobile.xml
    gridView = inflater.inflate(R.layout.pesquisa_2, null);

  // set value into textview
    TextView textView = (TextView) gridView.findViewById(R.id.grid_item_label);
     textView.setText(mobileValues[position]+"\n"+mobileValuesD[position]);

 // set image based on selected text
  ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_image);

  String mobile = mobileValues[position];

 //SELECT DAS IMAGENS

  if (mobile.equals("pdt1")) {
     imageView.setImageResource(R.drawable.img1);
  } else if (mobile.equals("prd2")) {
      imageView.setImageResource(R.drawable.feijao);
   } else {
       imageView.setImageResource(R.drawable.acucar);
    }


   } else {
    gridView = (View) convertView;
   }

    return gridView;
 }

   public int getCount() {
        return mobileValues.length;
 }

  public Object getItem(int position) {
      return null;
 }

   public long getItemId(int position) {
      return 0;
 }

 }
  • 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-04T13:25:41+00:00Added an answer on June 4, 2026 at 1:25 pm

    gridView.getItemAtPosition(position) calls the adapter’s getItem(int position) under the scene – so implement getItem in your ImageAdapter class to return sth that will allow you to identify what is selected (maybe mobileValues[position] is enough or just what you write into textview: mobileValues[position] + "\n" + mobileValuesD[position]).

    On the other hand side in onItemClick(AdapterView<?> parent, View v, int position, long id) you have allready have the position of selected item maybe it is enough information? what you are about to do in that listener?

    I have put some changes to your adapter:

    public class ImageAdapter extends BaseAdapter {
    
        private final String[] mobileValues;
        private final String[] mobileValuesD;
    
        public ImageAdapter(String[] mobileValues, String[] mobileValuesD) {
            this.mobileValues = mobileValues;
            this.mobileValuesD = mobileValuesD;
        }
    
        @Override
        public View getView(int position, View view, ViewGroup parent) {
            if (view == null) {
                view = ViewGroup.inflate(
                        parent.getContext(), R.layout.pesquisa_2, null);
            }
    
            ((TextView) view.findViewById(R.id.grid_item_label))
                    .setText(getItem(position));
    
            ((ImageView) view.findViewById(R.id.grid_item_image))
                    .setImageResource(getImageResForPosition(position));
    
            return view;
        }
    
        private int getImageResForPosition(int position) {
            String mobile = mobileValues[position];
            if (mobile.equals("pdt1")) {
                return R.drawable.img1;
            } else if (mobile.equals("prd2")) {
                return R.drawable.feijao;
            } else {
                return R.drawable.acucar;
            }
        }
    
        @Override
        public int getCount() {
            return mobileValues.length;
        }
    
        @Override
        public Object getItem(int position) {
            return mobileValues[position] + "\n" + mobileValuesD[position];
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had used the SqlDataSource to retrieve data from database to gridview. <asp:SqlDataSource ID=SqlDataSource1
I had used several ways to do some simple integer arithmetic in BASH (3.2).
I've got a simple ListView which pulls data from an ObservableCollection and I'm using
I had used a webgrid and displayed the data fine.For one column in the
I had used JExcel API for importing Data's from Excel,for using JExcel , I
I want to get zip code from users current location(Latitude, Longitude), I had used
i had sideshow ajax and i want it get images form database i used
I had used the following code to prevent the iPhone from entering the sleep
As I was typing some constants in Java I noticed I had used 4
I had used the date module in Drupal 6 for my custom module.But in

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.