如何使用onclicklistener进行网格视图

时间:2023-01-24 10:05:18

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

我使用gridview显示数字,所以如果我点击任何数字,下一个活动应该开始。我尝试了这段代码,但应用程序崩溃了

         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的xml代码

     <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

logcat文件日志

 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

3 个解决方案

#1


35  

GridView is Like a ListView

GridView就像一个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

有关参考,请参阅http://developer.android.com/reference/android/widget/GridView.html

#2


4  

You should use onItemClickListener instead of onClickListener.

您应该使用onItemClickListener而不是onClickListener。

#3


1  

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

gridView.onClickListener()将作为视图的网格侦听器。 gridView.setOnItemClickListener()将是网格中项目的侦听器。它会将该位置作为一个参数来指示您单击的项目。 parent参数将指示gridView本身。您可以使用它来基于项目的维度。就像是

view.setMinHeight(parent.getHeight()/n);

#1


35  

GridView is Like a ListView

GridView就像一个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

有关参考,请参阅http://developer.android.com/reference/android/widget/GridView.html

#2


4  

You should use onItemClickListener instead of onClickListener.

您应该使用onItemClickListener而不是onClickListener。

#3


1  

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

gridView.onClickListener()将作为视图的网格侦听器。 gridView.setOnItemClickListener()将是网格中项目的侦听器。它会将该位置作为一个参数来指示您单击的项目。 parent参数将指示gridView本身。您可以使用它来基于项目的维度。就像是

view.setMinHeight(parent.getHeight()/n);