4

Here is my buttonAdapter class that i think is accurate:

package com.example.test;

import android.content.Context; 
import android.graphics.Color; 
import android.view.View; 
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button; 
import android.widget.GridView;

public class ButtonAdapter extends BaseAdapter { 
    private Context mContext; 
    public String [] fName = { "File 1", "File 2", "Roflcopters"};

    // Gets the context so it can be used later 
    public ButtonAdapter(Context c) { mContext = c; }

    // Total number of things contained within the adapter 
    public int getCount () { return 8; }

    // Require for structure, not really used in my code. 
    public Object getItem (int position) {  return null; }

    // Require for structure, not really used in my code. Can be used to get the id of an item in the adapter for manual control. 
    public long getItemId (int position) { return position; }

    public View getView (int position, View convertView, ViewGroup parent){     
        Button btn;         
        if (convertView == null) {      // if it's not recycled, initialize some attributes      
            btn = new Button (mContext);
            btn.setLayoutParams (new GridView.LayoutParams (190, 190));
            btn.setPadding (1, 1, 1, 1);    
        } else {        
            btn = (Button) convertView;
        }       
      // btn.setText(filesnames[position]);   
      // filenames is an array of strings    
      //btn.setTextColor (Color.WHITE);
      //btn.setBackgroundResource (R.drawable.sample_2);  
      //btn.setBackgroundColor (Color.BLACK);     
      btn.setHighlightColor(Color.GREEN);    
      btn.setId (position);

      return btn; 
    } 
}

Here is my home class. I can't get the onItemClick to work out. What am I doing wrong here:

package com.example.test;

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View;
import android.view.View.OnClickListener; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.GridView; 
import android.widget.Toast;

public class home extends Activity implements OnItemClickListener {

    public final static String EXTRA_MESSAGE1 = "com.example.text.MESSAGE";

    public void onCreate (Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);     
        setContentView (R.layout.activity_home);    

        GridView gridview = (GridView) findViewById (R.id.gridview);
        gridview.setAdapter (new ButtonAdapter (this));     

        /*gridview.setOnItemClickListener (new OnItemClickListener () {
           public void onItemClick (AdapterView <?> parent, View v, int position, long id) {          
            Toast.makeText (home.this, "" + position, Toast.LENGTH_LONG).show ();
            Intent intent = new Intent (this, alarm.class);       
            String message = "Position:" + position;            
            intent.putExtra(EXTRA_MESSAGE1, message);        
            startActivity (intent);         
            }     
        });   
     * */    
     }

    @Override 
    public void onItemClick (AdapterView <?> parent, View v, int position, long id) {  
        Intent intent = new Intent (this, alarm.class);
        String message = "Position:" + position;        
        intent.putExtra(EXTRA_MESSAGE1, message);        
        startActivity (intent);  
    }
}

The onItemClick doesn't work and neither does the commented 'setOnItemClickListener' when it isn't commented out and 'onItemClick' is commented. What am I doing wrong?

AnujAroshA
  • 4,623
  • 8
  • 56
  • 99
user1594295
  • 51
  • 1
  • 1
  • 2

4 Answers4

5

If GridView, ListView have click able controls like BUtton, then onItemClick will not fired.
You need to implement Button Click listener in your getView method of the adapter.

like

public View getView(int position, View convertView, ViewGroup parent) {
    Button btn;
    if (convertView == null) { // if it's not recycled, initialize some
                                // attributes btn = new Button (mContext);
        btn.setLayoutParams(new GridView.LayoutParams(190, 190));
        btn.setPadding(1, 1, 1, 1);
    } else {
        btn = (Button) convertView;
    } // btn.setText(filesnames[position]); // filenames is an array of
        // strings //btn.setTextColor (Color.WHITE);
    // btn.setBackgroundResource (R.drawable.sample_2);
    // btn.setBackgroundColor (Color.BLACK);
    btn.setHighlightColor(Color.GREEN);
    btn.setId(position);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Handle the click here

        }
    });
    return btn;

}
Mohsin Naeem
  • 12,542
  • 3
  • 39
  • 53
  • Now what's the deal? It seems like my intent can't accept arguments? Why not? btn.setOnClickListener (new OnClickListener () { @Override public void onClick (View v) { // Toast msg = Toast.makeText (mContext, "Position:" + slot, Toast.LENGTH_SHORT); // msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2); // msg.show(); Intent intent = new Intent(this, AnAlarm.class); String message = "Position:" + slot; intent.putExtra(EXTRA_MESSAGE, message); mContext.startActivity(intent); }}); return btn; } – user1594295 Aug 13 '12 at 06:18
  • which `argument` it does not accept? `position` make it `final` – Mohsin Naeem Aug 13 '12 at 06:27
  • I'm trying to delete items from the gridview now. BaseAdapter.remove (position) isn't working. – user1594295 Aug 14 '12 at 02:48
3

You can add this line in the root layout of GridView items:

android:descendantFocusability="blocksDescendants"

Then onItemClickListener.onItemClick() will not fire when you tap on subviews which OnClickListener has been defined for them separately.

Mansour
  • 445
  • 1
  • 4
  • 14
  • Adding this to my grid view cell solved the issue. Both the button AND the grid cell became clickable separately. (Without it, just the button was clickable.) – dwsolberg Feb 24 '18 at 23:07
0

I've tested that Set Button.onClickListener() (in API 15) won't solve the problem.

So the GridView will not trigger onItemClick if it contains clickable views.

You can use ImageView instead Button.

herbertD
  • 10,657
  • 13
  • 50
  • 77
-1

i have the same problem when i was trying to implement onitemclick on gridview where filled with button. Because the button is stealing the focus of each space on gridview, you have to give the inflated button android:focusable="false" . however, the button is taking up almost entire space inside a grid, so you have to click the very edge of the button to trigger onitemclick call-back. i am suggesting you can set onclick or use image and designe it like a button.

Md. Sahadat Hossain
  • 3,210
  • 4
  • 32
  • 55