下面是我要创建的gridview的代码.App安装在设备中,但不幸的是显示停止错误
这是带适配器的网格代码
package com.anis.project_test1;
import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
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 Grid extends Activity {
static final String[] ITEM_LIST = new String[] { "Timetable", "Coursework",
"Exam" };
GridView gridView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this,ITEM_LIST ));
}
class ImageAdapter extends BaseAdapter
{
private Context context;
private final String[] itemName;
public ImageAdapter(Context context, String[] itemName) {
this.context = context;
this.itemName = itemName;
}
public int getCount() {
return itemName.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView = null;
if (convertView==null)
{
gridView = new View(context);
gridView = inflater.inflate(R.layout.griditem, null);
TextView textView = (TextView) gridView.findViewById(R.id.grid_label);
textView.setText(itemName[position]);
ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_image);
String item = itemName[position];
if (item.equalsIgnoreCase("timetable"))
{
imageView.setImageResource(R.drawable.timetable);
}
else if (item.equalsIgnoreCase("coursework"))
{
imageView.setImageResource(R.drawable.coursework);
}
else if (item.equalsIgnoreCase("exam"))
{
imageView.setImageResource(R.drawable.test);
}
else
{
gridView = (View) convertView;
}
return gridView;
}
return gridView;
}
}
}
这是我的gridview xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="@+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="spacingWidthUniform"
android:columnWidth="114dp" >
</GridView>
这是每个网格块中项目的xml文件。
<?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"
android:padding="5dp" >
<ImageView
android:id="@+id/grid_image"
android:layout_width="114dp"
android:layout_height="114dp"
android:layout_gravity="center"
android:src="@drawable/timetable" />
<TextView
android:id="@+id/grid_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="timetable"
android:textColor="#000000"
android:textSize="20dp" />
</LinearLayout>
这是我的清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.anis.project_test1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.anis.project_test1.Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Grid"></activity>
</application>
</manifest>
答案 0 :(得分:1)
请尝试这种方式,希望这有助于您解决问题。
public class Grid extends Activity{
static final String[] ITEM_LIST = new String[] { "Timetable", "Coursework",
"Exam" };
GridView gridView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this,ITEM_LIST ));
}
class ImageAdapter extends BaseAdapter
{
private Context context;
private final String[] itemName;
public ImageAdapter(Context context, String[] itemName) {
this.context = context;
this.itemName = itemName;
}
public int getCount() {
return itemName.length;
}
public Object getItem(int position) {
return itemName[position];
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView==null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.griditem, null);
holder.textView = (TextView) convertView.findViewById(R.id.grid_label);
holder.imageView = (ImageView) convertView.findViewById(R.id.grid_image);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.textView.setText(itemName[position]);
if (itemName[position].equalsIgnoreCase("timetable")){
holder.imageView.setImageResource(R.drawable.timetable);
}else if (itemName[position].equalsIgnoreCase("coursework")){
holder.imageView.setImageResource(R.drawable.coursework);
}else{
holder.imageView.setImageResource(R.drawable.test);
}
return convertView;
}
class ViewHolder{
TextView textView;
ImageView imageView;
}
}
}