简体   繁体   English

Cardview 多项活动

[英]Cardview Multiple activities

I working on Android studio 3.0.1.我在 Android Studio 3.0.1 上工作。 I working a project on android cardview.我在 android cardview 上做一个项目。 I see a video tutorial on android cardview.我在 android cardview 上看到了一个视频教程。 I write code carefully.我认真写代码。 But when I run code, it saw some error.但是当我运行代码时,它看到了一些错误。

在此处输入图片说明

Here's the corresponding code in my MainActivity.java这是我的MainActivity.java的相应代码

private void setSingleEvent(GridLayout mainGrid) {
    //Loop all child item of Main Grid
    for (int i = 0; i < mainGrid.getChildCount(); i++) {
        //You can see , all child item is CardView , so we just cast object to CardView
        CardView cardView = (CardView) mainGrid.getChildAt(i);
        final int finalI = i;
        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (finalI == 0) //Teachers Activities
                {
                    Intent intent = new Intent( packageContext: MainActivity.this, Teachers.class);
                    startActivity(intent);
                }

                else if (finalI == 1) //Students Activities
                {
                    Intent intent = new Intent( packageContext: MainActivity.this, Students.class);
                    startActivity(intent);
                }
                else if (finalI == 2) //Students Activities
                {
                    Intent intent = new Intent( packageContext: MainActivity.this, Students.class);
                    startActivity(intent);
                }
                else if (finalI == 3) //Notices Activities
                {
                    Intent intent = new Intent( packageContext: MainActivity.this, Notices.class);
                    startActivity(intent);
                }
                else if (finalI == 4) //Results Activities
                {
                    Intent intent = new Intent( packageContext: MainActivity.this, Results.class);
                    startActivity(intent);
                }
                else if (finalI == 5) //Phones Activities
                {
                    Intent intent = new Intent( packageContext: MainActivity.this, Phones.class);
                    startActivity(intent);
                }
                else if (finalI == 6) //Blood Activities
                {
                    Intent intent = new Intent( packageContext: MainActivity.this, Bloods.class);
                    startActivity(intent);
                }
                else {
                    Toast.makeText(MainActivity.this, "Activvvv", Toast.LENGTH_SHORT).show();
                }


            }
        });
    }
}

Full Source code on here 完整源代码在这里

Remove the packageContext which are red.删除红色的packageContext The final function should look like this.最终的函数应该是这样的。

private void setSingleEvent(GridLayout mainGrid) {
    //Loop all child item of Main Grid
    for (int i = 0; i < mainGrid.getChildCount(); i++) {
        //You can see , all child item is CardView , so we just cast object to CardView
        CardView cardView = (CardView) mainGrid.getChildAt(i);
        final int finalI = i;
        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (finalI == 0) //Teachers Activities
                {
                    Intent intent = new Intent(MainActivity.this, Teachers.class);
                    startActivity(intent);
                }

                else if (finalI == 1) //Students Activities
                {
                    Intent intent = new Intent(MainActivity.this, Students.class);
                    startActivity(intent);
                }
                else if (finalI == 2) //Students Activities
                {
                    Intent intent = new Intent(MainActivity.this, Students.class);
                    startActivity(intent);
                }
                else if (finalI == 3) //Notices Activities
                {
                    Intent intent = new Intent(MainActivity.this, Notices.class);
                    startActivity(intent);
                }
                else if (finalI == 4) //Results Activities
                {
                    Intent intent = new Intent(MainActivity.this, Results.class);
                    startActivity(intent);
                }
                else if (finalI == 5) //Phones Activities
                {
                    Intent intent = new Intent(MainActivity.this, Phones.class);
                    startActivity(intent);
                }
                else if (finalI == 6) //Blood Activities
                {
                    Intent intent = new Intent(MainActivity.this, Bloods.class);
                    startActivity(intent);
                }
                else {
                    Toast.makeText(MainActivity.this, "Activvvv", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

Android Studio complains because the following statement is syntactically wrong in Java: Android Studio 抱怨是因为以下语句在 Java 中在语法上是错误的:

Intent intent = new Intent( packageContext: MainActivity.this, Notices.class);

Android Studio shows parameter names (like "packageContext:") before method parameters to give Java code for Android a bit of a Kotlin look and feel. Android Studio 在方法参数之前显示参数名称(如“packageContext:”),为 Android 的 Java 代码提供一点 Kotlin 外观和感觉。 This can be confusing for beginners.这可能会让初学者感到困惑。 The correct form of the statement is:该语句的正确形式是:

Intent intent = new Intent(MainActivity.this, Notices.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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