1. Download our Official Android App: Forums for Android!

How to get the position of the imageButton clicked, contained in the gridview control?

Discussion in 'Introductions' started by fashaikh, Dec 6, 2010.

  1. fashaikh

    fashaikh Lurker
    Thread Starter
    Rank:
    None
    Points:
    25
    Posts:
    1
    Joined:
    Nov 24, 2010

    Nov 24, 2010
    1
    0
    25
    Hi!

    I have a gridview control in which i have populated imagebuttons.
    For the click event of the imagebutton , I have implemented its onItemClick in the binding class of the gridview rather than using the setOnItemClick of the gridview since it was not firing in case of the imagebutton though previously it was firing in the case of the imageview as described in the online tutorial for gridview in developer.android.com/resources.

    Now the problem is that the gridview's

    Code (Text):
    1.  
    2.  
    3. ButtonAdaptor buttonAdaptor = new ButtonAdaptor( this.getContext() );
    4. grid.setAdapter( buttonAdaptor );
    5. grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    6.         public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    7.             Toast.makeText(getContext(), "" + position, Toast.LENGTH_SHORT).show();
    8.         }
    9.  
    this doesnt fire for my class ButtonAdaptor which contains the code for ImageButtons.
    I can see that the listener event has a integer position argument so this is what I want.

    But what fires for me is the code in the ButtonAdaptor class

    Code (Text):
    1.  
    2. public View getView (int index, View convertView, ViewGroup parent)
    3. {
    4. ImageButton imageButton;
    5.  
    6. if (convertView == null) {  // if it's not recycled, initialize some attributes
    7.                 imageButton = new ImageButton( getContext() );
    8.                 imageButton.setLayoutParams( new GridView.LayoutParams( T.px(72), T.px(72) ) );
    9.                 imageButton.setScaleType( ImageView.ScaleType.CENTER_CROP );
    10.                 T.setPadding( imageButton, 5, 5, 5, 10 );
    11. }
    12. else {
    13.         imageButton = (ImageButton) convertView;
    14. }
    15.  
    16. imageButton.setImageDrawable( Resource.getImage( getItem(index).toString().toLowerCase(), true ));
    17.             imageButton.setOnClickListener(this);  // attach the onClickListener //method
    18.            
    19.     return imageButton;
    20. }
    21.  
    22. public void onClick (View view) {
    23.  
    24. //This event fires for me but doesnt give me the position of the imagebutton clicked
    25. }
    26.  
    So what should I do?

    Kindly help me out

    regards
    FAS
     

    Advertisement

  2. Martimus

    Martimus One bite at a time...
    Rank:
    None
    Points:
    843
    Posts:
    19,535
    Joined:
    Jul 9, 2010

    Jul 9, 2010
    19,535
    4,045
    843
    Male
    Engineer
    'neath a cactus

Share This Page

Loading...