Android gridview OnItemClickListener

20,249

Solution 1

Change this

Toast.makeText(getApplicationContext(),
                    "Item Clicked: " + position, Toast.LENGTH_SHORT).show();

to

Toast.makeText(getApplicationContext(),
                        "Item Clicked: " + ((TextView) v.findViewById(R.id.item_text)).getText(), Toast.LENGTH_SHORT).show();

Solution 2

I know this answer is coming in a little late but I just ran into the same issue and I solved it by using tags on the clickable items using setTag() and getTag(). I just want to post this method in case it will help someone else in the future. In order to use this method and the view "holding pattern" (like you are using above) you just have to set the tag on another item inside the object that is "holding" your data.

For example:

You already have a public RecordHolder object with a public TextView so you can set the tag on this TextView like so:

holder.txtTitle.setTag("whatever you want")

Then inside your OnItemClickListener.onItemClick method you can get the tag like so:

String tagValue = ((CustomGridViewAdapter.RecordHolder) view.getTag()).txtTitle.getTag().toString();

tagValue will be whatever you want (so you can assign the names of the views if you wish). This method works well if you want to store other data (like ids or references) and not just grab the text of TextView or something like this. If you just want the same text that is in TextView then @Giru's answer is more fitting.

Share:
20,249
user3619713
Author by

user3619713

Updated on February 02, 2020

Comments

  • user3619713
    user3619713 over 4 years

    I successfully created a custom gridview(with images and texts). Now I want to write an OnItemClickListener method.I can get the position on OnItemClickListener method, but I want to be able to click "Friend" and see "Item clicked : Friend"

    How can I get the select string instead of the position? Here is my source code

    public class Item {
    Bitmap image;
    String title;
    
    public Item(Bitmap image, String title) {
        super();
        this.image = image;
        this.title = title;
    }
    public Bitmap getImage() {
        return image;
    }
    public void setImage(Bitmap image) {
        this.image = image;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    
    
    
    public class CustomGridViewAdapter extends ArrayAdapter<Item> {
    Context context;
    int layoutResourceId;
    ArrayList<Item> data = new ArrayList<Item>();
    public static Item item ;
    
    public CustomGridViewAdapter(Context context, int layoutResourceId,
            ArrayList<Item> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        RecordHolder holder = null;
    
        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
    
            holder = new RecordHolder();
            holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
            holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
            row.setTag(holder);
        } else {
            holder = (RecordHolder) row.getTag();
        }
    
        item= data.get(position);
        holder.txtTitle.setText(item.getTitle());
        holder.imageItem.setImageBitmap(item.getImage());
        return row;
    
    }
    
    public static class RecordHolder {
        public static TextView txtTitle;
        ImageView imageItem;
    
    }
    

    }

    public class MainActivity extends Activity {
    GridView gridView;
    ArrayList<Item> gridArray = new ArrayList<Item>();
    CustomGridViewAdapter customGridAdapter;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        // set grid view item
        Bitmap homeIcon = BitmapFactory.decodeResource(this.getResources(),
                R.drawable.home);
        Bitmap userIcon = BitmapFactory.decodeResource(this.getResources(),
                R.drawable.personal);
    
        gridArray.add(new Item(homeIcon, "Home"));
        gridArray.add(new Item(userIcon, "User"));
        gridArray.add(new Item(homeIcon, "House"));
        gridArray.add(new Item(userIcon, "Friend"));
        gridArray.add(new Item(homeIcon, "Home"));
        gridArray.add(new Item(userIcon, "Personal"));
        gridArray.add(new Item(homeIcon, "Home"));
        gridArray.add(new Item(userIcon, "User"));
        gridArray.add(new Item(homeIcon, "Building"));
        gridArray.add(new Item(userIcon, "User"));
        gridArray.add(new Item(homeIcon, "Home"));
        gridArray.add(new Item(userIcon, "xyz"));
    
        gridView = (GridView) findViewById(R.id.gridView1);
        customGridAdapter = new CustomGridViewAdapter(this, R.layout.row_grid,
                gridArray);
        gridView.setAdapter(customGridAdapter);
        gridView.setOnItemClickListener(new OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Toast.makeText(getApplicationContext(),
                        "Item Clicked: " + position, Toast.LENGTH_SHORT).show();
    
            }
        });
    }
    

    }

  • user3619713
    user3619713 almost 10 years
    thanks but it is incorrent answer.i changed this code ,but i can not show for example home if i click home :(
  • user3619713
    user3619713 almost 10 years
    i also deleted static in item object but when i click item in gridview i can show package name and like this text @556622123
  • user3619713
    user3619713 almost 10 years
    when i click item in gridview i have like this result package name Item@5563323
  • Tascalator
    Tascalator almost 10 years
    Not sure if it could work but cant you do .getItem(position).Text instead to get the object's text and not the object's string representation