您好,GridView 教程不起作用

发布于 2024-09-24 05:43:41 字数 2889 浏览 0 评论 0原文

我已按照教程进行操作,但似乎无法使其工作。这是我的代码:

GridView.java:

package com.example.gridview;

import android.app.Activity;
import android.os.Bundle;

public class GridView extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });
}

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7
    };

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"

我得到的都是错误消息。我做错了什么?我遵循了教程所说的一切。

I have followed the tutorial and can't seem to get it to work. Here's my code:

GridView.java:

package com.example.gridview;

import android.app.Activity;
import android.os.Bundle;

public class GridView extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });
}

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7
    };

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"

All I get are error message everywhere. What did I do wrong? I followed everything the tutorial said to do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

拥抱我好吗 2024-10-01 05:43:41

问题是,就像马特说的那样,您的活动名称是“GridView”,并且您正在调用 Toast.makeText(HelloGridView.this,... --> “GridView”与“HelloGridView”不同,并且它们必须具有相同的名称。

Ctrl+Shift+O 也有帮助,因为这将为代码添加所有必要的导入。

The problem is, just like Matt said, the name of your activity is "GridView", and you're calling Toast.makeText(HelloGridView.this,... --> "GridView" is different that "HelloGridView", and they have to be the same name.

Ctrl+Shift+O helps too, because that will add all the necessary imports for the code.

夏末染殇 2024-10-01 05:43:41

截至 2011 年 12 月,当我刚刚查看时,Android 文档中的这个示例仍然不包含适当的命名空间。因此,如上所述,在活动类中单击 ctrl -shift - o 将导入所有适当的命名空间。

然而,除了这个然后在这条线上
Toast.makeText(HelloGridView.this, "" + 位置, Toast.LENGTH_SHORT).show();

您需要确保 HelloGridView 是您正在使用的实际 Activity 类的名称。例如,我的类的名称实际上是 HelloGridViewActivity,因此这一行对我来说是错误的。

最后,当您创建 imageadapter.java 类时,只需确保该类与您的活动 java 类位于同一名称空间中,例如,testing.examples.HelloGridView。

这 3 项允许我运行示例。

This example in the android documentation does not include appropriate namespaces still as of december 2011 when I just looked at it. So, as mentioned above clicking ctrl -shift - o in your activity class will import all of the appropriate namespaces.

However, in addition to this then on this line
Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();

you need to make sure that HelloGridView is the name of the actual Activity class that you are working in. For instance, the name of my class was actually HelloGridViewActivity and so this line was erroring out for me.

And finally, when you create the imageadapter.java class just make sure that this class is in the same namespace as your activity java class, ie testing.examples.HelloGridView for instance.

These 3 items allowed me to run the sample.

暮年 2024-10-01 05:43:41

我也遇到了一点问题。

为了解决所有错误,我只需在 Eclipse 中按 ctrl-shift-o 即可导入所有内容。
然后在我的 ImageAdapter.java 中,我确保我在顶部定义了包。
所以请确保包 com.example.gridview;在您的 ImageAdapter.java 中

希望有帮助。

I was having a wee bit of a problem with this too.

To solve all the errors I just pressed ctrl-shift-o in eclipse to import everything.
Then in my ImageAdapter.java I made sure I defined the package at the top.
So make sure package com.example.gridview; is in your ImageAdapter.java

Hope that helps.

这个俗人 2024-10-01 05:43:41

所以我遇到了同样的问题,在每个页面上按 Ctrl+Shift+O 后,然后在 HelloGridViewActivity.java 类的最底部将 HelloGridView 更改为 HelloGridViewActivity 后,我只剩下可绘制文件夹问题。

在本教程中,它使用了一个可绘制文件夹。您只能获得一个drawable-hdpi、drawable-mdpi 和drawable-ldpi,因此您必须在res 文件夹中创建一个名为drawable 的新文件夹才能使代码正常工作。如果没有,您可以随时将其放入drawable-hdpi文件夹中并进行查找/替换。

祝你好运!

So I ran into the same issue, and after hitting Ctrl+Shift+O on each page, then after I changed the HelloGridView to HelloGridViewActivity at the very bottom on the HelloGridViewActivity.java class, I was only left with the drawable folder issues.

In the tutorial it uses a drawable folder. You only get a drawable-hdpi, drawable-mdpi, and drawable-ldpi, so you have to create a new folder in your res folder called drawable in order for the code to work. If not, you can always put it in the drawable-hdpi folder and do a find/replace.

Good luck!

慕烟庭风 2024-10-01 05:43:41

您可以转到项目并选择“Android Tool->Project Properties”,然后清理项目“Project ->Clean”吗?
如果仍然出现,进入AndroidManifest.xml检查是否有错误

Can you just go to project and select "Android Tool->Project Properties" then clean up the project "Project ->Clean".
If it still occurs, go to the AndroidManifest.xml and check if there are any errors

极度宠爱 2024-10-01 05:43:41

ImageAdapter类中,getItemgetItemId返回必须是位置。您可以在那里放置位置并重试。

http://www.mkyong.com/android/android-gridview-example/

In ImageAdapter class getItem and getItemId return must be position. You can put position there and try again.

http://www.mkyong.com/android/android-gridview-example/

予囚 2024-10-01 05:43:41

您尚未导入所有必需的类,并且 Eclipse 有时不使用通过单击来导入类。

按 (Ctrl+Shift+o) 导入所有必需的类
我修复它的地方

MainActivity.java

  // import all packages:
 import android.app.Activity;
 import android.os.Bundle;
 import android.widget.Toast;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.GridView;
 import android.widget.AdapterView.OnItemClickListener;

 public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(getApplicationContext()));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick1(AdapterView<?> parent, View v,
                int position, long id) {
            Toast.makeText(MainActivity.this, "" + position,
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

        }
    });
}

}

这会有所帮助。
继续编码..!!

You have not import all necessary classes and Eclipse sometimes don't use to import classes by clicking.

Press (Ctrl+Shift+o) to import all necessary classes
where i fixed it

MainActivity.java

  // import all packages:
 import android.app.Activity;
 import android.os.Bundle;
 import android.widget.Toast;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.GridView;
 import android.widget.AdapterView.OnItemClickListener;

 public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(getApplicationContext()));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick1(AdapterView<?> parent, View v,
                int position, long id) {
            Toast.makeText(MainActivity.this, "" + position,
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

        }
    });
}

}

this will help.
keep Coding..!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文