Android 在GridView中显示图像时出现问题
我正在尝试创建一个活动页面,该页面有一个可滚动的gridview,可以显示图像,但在运行应用程序时遇到了问题。这些图像没有显示,我也不确定我缺少了什么,以便在我从手机上运行应用程序进行测试时显示出来。我查找了基于代码的类似教程,但它仍然不起作用。非常感谢任何帮助或输入,如果我需要提供任何其他代码,请让我知道。多谢各位 xml文件activity_third.xmlAndroid 在GridView中显示图像时出现问题,android,image,gridview,Android,Image,Gridview,我正在尝试创建一个活动页面,该页面有一个可滚动的gridview,可以显示图像,但在运行应用程序时遇到了问题。这些图像没有显示,我也不确定我缺少了什么,以便在我从手机上运行应用程序进行测试时显示出来。我查找了基于代码的类似教程,但它仍然不起作用。非常感谢任何帮助或输入,如果我需要提供任何其他代码,请让我知道。多谢各位 xml文件activity_third.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmln
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
android:id="@+id/grid_view"
android:numColumns="2"
android:gravity="center"
android:columnWidth="50dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:smoothScrollbar="true"
>
</GridView>
</LinearLayout>
ImageAdapter.java
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
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.balloon_1, R.drawable.balloon_2,
R.drawable.balloon_3, R.drawable.balloon_4,
R.drawable.balloon_5, R.drawable.balloon_6
/*insert array of images*/
};
}
更改适配器类的以下方法
public Object getItem(int position) {
return mThumbIds[position];
}
public long getItemId(int position) {
return position;
}
返回的是null而不是数组位置。检查一次,将其更改为
public Object getItem(int position) {
return mThumbIds[position];
}
你有什么错误吗?如果是这样,那么请张贴错误也。谢谢!对不起,我是android新手,但如果你不介意的话,我还有一个问题。这些图像应该显示在我的图形布局上,还是应该在我运行应用程序在手机上测试时显示?因为我似乎仍然没有任何显示,我将图像添加到我的绘图工具中hdpi@nikkignarr只有在运行程序时才会显示图像。因为它只会在运行时绑定数据。
public Object getItem(int position) {
return mThumbIds[position];
}