Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use onclicklistener for grid view

Tags:

java

android

xml

i used gridview to display numbers, so if i click on any number the next activity should start. i tried this code but the app crashes

         private GridView gridView = null;

      gridView.setOnClickListener(new OnClickListener()

    {
              public void onClick(View v5) 

        {
            setContentView(R.layout.Abc);
            Intent myIntent = new 

 Intent(getApplicationContext(),Abc.class);
            startActivity(myIntent);

        }
    });

here is the xml code for gridview

     <GridView
    android:id="@+id/month_gridView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/calendar_days"
    android:layout_marginLeft="7dp"
    android:layout_marginRight="8dp"
    android:background="@color/grid_background"
    android:fadingEdge="none"
    android:gravity="top|left"
    android:horizontalSpacing="2dp"
    android:listSelector="@android:color/transparent"
    android:numColumns="7"
    android:padding="1dp"
    android:stretchMode="columnWidth"
    android:verticalSpacing="2dp">
</GridView>

logcat file log

 02-04 00:10:50.603: D/AndroidRuntime(341): Shutting down VM
    02-04 00:10:50.603: W/dalvikvm(341): threadid=1: thread exiting with uncaughtexception(group=0x40015560)
    02-04 00:10:50.635: E/AndroidRuntime(341): FATAL EXCEPTION: main
    02-04 00:10:50.635: E/AndroidRuntime(341): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.indianic.demo.calendark/com.indianic.demo.calendark.CalendarActivity}: java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
    02-04 00:10:50.635: E/AndroidRuntime(341):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at android.os.Handler.dispatchMessage(Handler.java:99)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at android.os.Looper.loop(Looper.java:123)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at java.lang.reflect.Method.invokeNative(Native Method)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at java.lang.reflect.Method.invoke(Method.java:507)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at dalvik.system.NativeStart.main(Native Method)
    02-04 00:10:50.635: E/AndroidRuntime(341): Caused by: java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
    02-04 00:10:50.635: E/AndroidRuntime(341):  at android.widget.AdapterView.setOnClickListener(AdapterView.java:750)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at com.indianic.demo.calendark.CalendarActivity.onCreate(CalendarActivity.java:126)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
    02-04 00:10:50.635: E/AndroidRuntime(341):  ... 11 more
    02-04 00:10:53.203: I/Process(341): Sending signal. PID: 341 SIG: 9
like image 604
Metalhead1247 Avatar asked Feb 03 '13 18:02

Metalhead1247


2 Answers

GridView is Like a ListView

You should use some thing like this

gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            // DO something

        }
    });

the code is not perfect

for reference see http://developer.android.com/reference/android/widget/GridView.html

like image 179
QAMAR Avatar answered Sep 25 '22 19:09

QAMAR


You should use onItemClickListener instead of onClickListener.

like image 23
Michał Z. Avatar answered Sep 25 '22 19:09

Michał Z.