如何使用onclicklistener网格视图 [英] how to use onclicklistener for grid view

查看:71
本文介绍了如何使用onclicklistener网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用GridView控件来显示数字, 所以,如果我点击任意数量的下一个活动的启动时间。 我想这code,但应用程序崩溃

 私人GridView控件GridView控件= NULL;

      gridView.setOnClickListener(新OnClickListener()

    {
              公共无效的onClick(查看V5)

        {
            的setContentView(R.layout.Abc);
            意图myIntent =新

 意图(getApplicationContext(),Abc.class);
            startActivity(myIntent);

        }
    });
 

下面是XML $ C $下的GridView

 < GridView控件
    机器人:ID =@ + ID / month_gridView
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_below =@ + ID / calendar_days
    机器人:layout_marginLeft =7DP
    机器人:layout_marginRight =8DP
    机器人:背景=@色/ grid_background
    机器人:fadingEdge =无
    机器人:重力=顶|左
    机器人:horizo​​ntalSpacing =2DP
    机器人:listSelector =@机器人:彩色/透明
    机器人:为numColumns =7
    机器人:填充=1DP
    机器人:stretchMode =columnWidth中
    机器人:verticalSpacing =2DP>
< / GridView的>
 

logcat的 文件日志

  02-04 00:10:50.603:D / AndroidRuntime(341):关闭虚拟机
    02-04 00:10:50.603:W / dalvikvm(341):主题ID = 1:螺纹uncaughtexception退出(组= 0x40015560)
    02-04 00:10:50.635:E / AndroidRuntime(341):致命异常:主要
    02-04 00:10:50.635:E / AndroidRuntime(341):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.indianic.demo.calendark / com.indianic.demo.calendark.CalendarActivity}:java.lang中.RuntimeException:不要叫setOnClickListener了一个AdapterView。你可能想setOnItemClickListener代替
    02-04 00:10:50.635:E / AndroidRuntime(341):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
    02-04 00:10:50.635:E / AndroidRuntime(341):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
    02-04 00:10:50.635:E / AndroidRuntime(341):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
    02-04 00:10:50.635:E / AndroidRuntime(341):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
    02-04 00:10:50.635:E / AndroidRuntime(341):在android.os.Handler.dispatchMessage(Handler.java:99)
    02-04 00:10:50.635:E / AndroidRuntime(341):在android.os.Looper.loop(Looper.java:123)
    02-04 00:10:50.635:E / AndroidRuntime(341):在android.app.ActivityThread.main(ActivityThread.java:3683)
    02-04 00:10:50.635:E / AndroidRuntime(341):在java.lang.reflect.Method.invokeNative(本机方法)
    02-04 00:10:50.635:E / AndroidRuntime(341):在java.lang.reflect.Method.invoke(Method.java:507)
    02-04 00:10:50.635:E / AndroidRuntime(341):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
    02-04 00:10:50.635:E / AndroidRuntime(341):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    02-04 00:10:50.635:E / AndroidRuntime(341):在dalvik.system.NativeStart.main(本机方法)
    02-04 00:10:50.635:E / AndroidRuntime(341):由:java.lang.RuntimeException的:不要叫setOnClickListener了一个AdapterView。你可能想setOnItemClickListener代替
    02-04 00:10:50.635:E / AndroidRuntime(341):在android.widget.AdapterView.setOnClickListener(AdapterView.java:750)
    02-04 00:10:50.635:E / AndroidRuntime(341):在com.indianic.demo.calendark.CalendarActivity.onCreate(CalendarActivity.java:126)
    02-04 00:10:50.635:E / AndroidRuntime(341):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    02-04 00:10:50.635:E / AndroidRuntime(341):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
    02-04 00:10:50.635:E / AndroidRuntime(341):11 ...更多
    02-04 00:10:53.203:I /处理(341):发送信号。 PID:341 SIG:9
 

解决方案

GridView控件就像一个ListView

您应该使用一些这样的事

  gridView.setOnItemClickListener(新OnItemClickListener(){
        @覆盖
        公共无效onItemClick(适配器视图<>母公司,视图V,
                INT位置,长的id){

            // 干点什么

        }
    });
 

在code是不完美

有关更多信息参见<一href="http://developer.android.com/reference/android/widget/GridView.html">http://developer.android.com/reference/android/widget/GridView.html

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

这篇关于如何使用onclicklistener网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆