我试图练习在Android Studio中使用GridView,我不知道为什么我会一直得到这个例外。
这是我主要课程的代码。
package com.example.chetam.gokalsapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
public class Products extends Activity {
GridView grid;
String[] web = {
"Google",
"Github",
"Instagram",
"Facebook",
"Flickr",
"Pinterest",
"Quora",
"Twitter",
"Vimeo",
"WordPress",
"Youtube",
"Stumbleupon",
"SoundCloud",
"Reddit",
"Blogger"
} ;
int[] imageId = {
R.drawable.v,
R.drawable.img,
R.drawable.img1,
R.drawable.img2,
R.drawable.img3,
R.drawable.v,
R.drawable.img,
R.drawable.img1,
R.drawable.img2,
R.drawable.img3,
R.drawable.v,
R.drawable.img,
R.drawable.img1,
R.drawable.img2,
R.drawable.img3
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_products);
grid=(GridView)findViewById(R.id.grid);
CustomGrid adapter = new CustomGrid(Products.this, web, imageId);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(Products.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
}
});
}
}
以下是我的主要XML文件的代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.chetam.gokalsapp.Products">
<GridView
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/grid"
/>
</RelativeLayout>
这是我的布局文件
<?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:padding="5dp"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:id="@+id/grid_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="15dp">
</ImageView>
<TextView
android:id="@+id/grid_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="12sp" >
</TextView>
</LinearLayout>
这是我的适配器类
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomGrid extends BaseAdapter{
private Context mContext;
private final String[] web;
private final int[] Imageid;
public CustomGrid(Context c,String[] web,int[] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.web = web;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return web.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid_single, null);
TextView textView = (TextView) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8,8,8,8);
textView.setText(web[position]);
imageView.setImageResource(Imageid[position]);
}
else
{
grid = (View) convertView;
}
return grid;
}
}
答案 0 :(得分:0)
将网格onClickListener替换为:
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(Products.this, "You Clicked at " +web[position], Toast.LENGTH_SHORT).show();
}
});
您正在使用整数变量作为String数组的位置。如果你有'+ position'因为你想要一个比位置更高的数字,那么正确的语法是:
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(Products.this, "You Clicked at " +web[position + 1], Toast.LENGTH_SHORT).show();
}
});