简体   繁体   中英

how to use onclicklistener for grid view

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

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

您应该使用onItemClickListener而不是onClickListener。

gridView.onClickListener() would be the listener for the grid as a view. gridView.setOnItemClickListener() would be the listener for an item in the grid. It would take the position as a parameter that would indicate the item you are clicking on. The parent parameter would indicate the gridView itself. You could use this to base the dimension of the item. Something like

view.setMinHeight(parent.getHeight()/n);
 gridLayout_topCategory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                 fragmentManager =getParentFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.fragment_container, new FragCategory(getContext(),fragmentManager),"fragCategory").addToBackStack(null).commit();
                Home.ll_bottomTabBarLayout.setVisibility(View.GONE);
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM