gpt4 book ai didi

android - 在 GridView 中显示图片

转载 作者:行者123 更新时间:2023-11-29 18:21:52 25 4
gpt4 key购买 nike

我正在尝试使用 Gridview 显示放置在可绘制文件夹中的图片。我收到一些错误。我的代码和我遇到的错误如下所示。

package com.newapp;

import android.R;
import android.R.drawable;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;/*Error here: main cannot be resolved or is not a field*/

import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);/*Error here: main cannot be resolved or is not a field*/

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

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


}

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_0, /*Error here: sample_0 cannot be resolved and is not a field*/
R.drawable.sample_1,/*Error here: sample_1 cannot be resolved and is not a field*/
R.drawable.sample_2,/*Error here: sample_2 cannot be resolved and is not a field*/
R.drawable.sample_3,/*Error here: sample_3 cannot be resolved and is not a field*/
R.drawable.sample_4,/*Error here: sample_4 cannot be resolved and is not a field*/
R.drawable.sample_5/*Error here: sample_5 cannot be resolved and is not a field*/,
R.drawable.sample_6,
/*Error here: sample_6 cannot be resolved and is not a field*/
};
}

那么有人可以告诉我是什么导致了这些问题吗?任何帮助都感激不尽。提前致谢

最佳答案

这是你的第一个问题:import android.R

不要导入这个,因为这是android系统的R包,而不是你自己项目的R文件,因此无法识别main,因为你在android.R包中指的main的R不存在。

如果删除此导入,“main”将被识别,因为将引用您自己的 R 文件。同样的规则适用于 ImageAdapter。如果您删除导入:import android.R.drawable,您将避免在此类中遇到的问题。

关于android - 在 GridView 中显示图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4952121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com