package com.zhouweiyoushale;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.zhouweiyousha.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
private List<GridItem> list;
private GridView MyGridView;
int[] image = { R.drawable.bank, R.drawable.bar, R.drawable.bus,
R.drawable.cinema, R.drawable.coffee, R.drawable.food,
R.drawable.gas_station, R.drawable.gong_gong_ji_gou,
R.drawable.hospital, R.drawable.hotel, R.drawable.other,
R.drawable.p, R.drawable.shopping, R.drawable.store,
R.drawable.toilet };
int[] name = { R.string.a, R.string.b, R.string.c, R.string.d, R.string.e,
R.string.f, R.string.g, R.string.h, R.string.j, R.string.l,
R.string.m, R.string.o, R.string.q, R.string.s, R.string.v
};
public void onCreat(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
intData();
setContentView(R.layout.main_activity);
MyGridView = (GridView) findViewById(R.id.MyGridView);
ItemBaseAdapter adapter = new ItemBaseAdapter(this, list);
MyGridView.setAdapter(adapter);
}
public void intData() {
List<GridItem> list = new ArrayList<GridItem>();
for (int i = 0; i < image.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("imageView", image[i]);
map.put("title", name[i]);
list.add((GridItem) map);
}
}
private class ItemBaseAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
private List<GridItem> list;
private ViewHolder holder;
public ItemBaseAdapter(Context context, List<GridItem> list) {
super();
this.mContext = context;
this.list = list;
mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return list.size();
}
public Object getItem(int position) {
return list.get(position);
}
public long getItemId(int position) {
return position;
}
class ViewHolder {
private TextView title;
private ImageView imageView;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (holder == null) {
convertView = mInflater.inflate(R.layout.item, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.imageView = (ImageView) convertView
.findViewById(R.id.imageView);
convertView.setTag(convertView);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(list.get(position).getTitle());
holder.imageView.setImageResource(list.get(position).getImageId());
return convertView;
}
}
class GridItem {
private String name;
private int imageId;
public GridItem(String title, int imageId) {
super();
this.name = title;
this.imageId = imageId;
}
public String getTitle() {
return name;
}
public void setTitle(String title) {
this.name = title;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zhouweiyousha"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="11" />
<application
android:allowBackup="true"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zhouweiyousha"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="11" />
<application
android:allowBackup="true"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="4dp"
android:scaleType="fitXY" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_vertical" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:background="#FFFFFF"
android:orientation="vertical" >
<GridView
android:id="@+id/MyGridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="10dp"
android:background="#FFFFFF"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</GridView>
</LinearLayout>
哪里错了啊岌岌可危了 工作试用期
------解决思路----------------------
感觉你那个holder用反了,当convertView==null时,创建view和holder