Devraj
Devraj

Reputation: 1531

Making GridView clickable in android

I am working on GridView and having so many items in it. here is my gridview.xml file

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

<GridView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/gridview"
    android:numColumns="auto_fit"
    android:horizontalSpacing="5dp"
    android:verticalSpacing="5dp"
    android:layout_margin="5dp"
    android:background="#FF8040"
    android:clickable="true"/>
</LinearLayout>

and this is gridview_layout.xml file that contains element to be displayed in gridview..

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp"
    android:background="#FFFFFF">


   <RelativeLayout 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/rel1">

    <ImageView 
            android:id="@+id/flag"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"/>


    <TextView 
            android:id="@+id/txt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="15dp"
            android:gravity="center_horizontal" 
            android:layout_below="@+id/flag"/>

    <RatingBar 
        android:id="@+id/rting1"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_below="@+id/txt"
        android:numStars="5" 
        style="@style/foodRatingBar"/>

    <LinearLayout 
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:orientation="horizontal"
        android:layout_below="@+id/rting1"
        android:weightSum="2">

        <Button 
            android:layout_width="0dp"
            android:layout_height="24dp"
            android:id="@+id/btnn1"
            android:text="Details"
            android:background="#A0A0A0"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"
            android:gravity="center"/>
        <Button 
            android:layout_width="0dp"
            android:layout_height="24dp"
            android:id="@+id/btnn2"
            android:text="Enquiry" 
            android:background="#A0A0A0"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"/>
    </LinearLayout>

    </RelativeLayout>


</LinearLayout>

I am using the standard OnItemClickListner() but this is not working. There is no error in logCat. Below is my GridViewActivity..

    public class GridViewActivity extends Activity{

    GridView gridView;
    Context context;
    ArrayList itemName;
    Button btn11;

    public static String [] web={"Butterfly Valve","Globe Valve","Piston Valve","Forged Globe Valve","Swing Non Return Valve","Wafer Non Return Valve","Diapgragm Valve","Three Way Ball Valve","Pp And Pvc Ball Valve",
        "Forged Ball Valve","Wafer Ball Valve","Pocket Less Ball Valve","Knife Edge Gate Valve"};

    public static int [] imageId={R.drawable.image1,
        R.drawable.image1,
        R.drawable.image1,
        R.drawable.image1,
        R.drawable.image1,
        R.drawable.image1,
        R.drawable.image1,
        R.drawable.image1,
        R.drawable.image1,
        R.drawable.image1}; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gridview);
        CustomGrid adapter = new CustomGrid(GridViewActivity.this, web, imageId);
        gridView = (GridView)findViewById(R.id.gridview);
        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(GridViewActivity.this, "you clicked at..." +web[ + position], Toast.LENGTH_LONG).show();

            }
        });

    }
}

and finally this is my adapter class that is CustomGrid:-

    public class CustomGrid extends BaseAdapter{

    private Context mContext;
    private final String[] web;
    private final int[] imageId;


    public CustomGrid(Context c,String[] web,int[] imageId ) {
        mContext = c;
        this.imageId = imageId;
        this.web = web;
    }

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

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View grid;
        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(convertView == null){
            grid = new View(mContext);

            grid = inflater.inflate(R.layout.gridview_layout, null);
            TextView textview = (TextView)grid.findViewById(R.id.txt);
            ImageView imageview = (ImageView)grid.findViewById(R.id.flag);
            textview.setText(web[position]);
            imageview.setImageResource(imageId[position]);
          } else {
            grid = (View) convertView;
          }

        return grid;
    }

}

Upvotes: 0

Views: 4520

Answers (2)

MohanRaj
MohanRaj

Reputation: 662

Your application crashes because btn11 is belongs to gridview_layout.xml. But you have set gridview.xml as your content view. You can get reference to only the views belongs to a xml layout which you set up in setContentView(layoutResID)

Its seems like you messed up things.

If you are looking for a customized grid view then have a look at bellow example.

http://www.learn2crack.com/2014/01/android-custom-gridview.html

Upvotes: 1

Carnal
Carnal

Reputation: 22064

Why your onItemClickListener is not working, might be because of your Buttons which gain focus for each item. Try setting focusable="false" to your Buttons for each item in gridview_layout.xml.

However, the crash you are getting is a NullPointerException (next time, make sure to post Logcat so other can help easily), because there is no id with btnn1 in gridview.xml which you are refering to. That id is inside gridview_layout.xml.

So you need to create a custom BaseAdapter and set that do be your Adapter for the GridView. Then in your overriden getView method inside this custom BaseAdapter, you will have get the Button like you failed to do.

Here's an example of creating a BaseAdapter for GridView.

http://www.mkyong.com/android/android-gridview-example/

Upvotes: 1

Related Questions