// ① 创建自定义布局文件
//② 创建自定义适配器
//③ 创建Activity.xml 布局文件
// ④ 写MainActivity方法 准备数据源
//⑤ 构建适配器对象
// ⑥ 给布局文件添加适配器
// ⑦ 页面跳转
//⑧ 将选中资源id 返回 设置头像
自定义适配器
package com.example.gp11_day12_gridview2.adatper;
import java.util.List;
import java.util.Map;
import com.example.gp11_day12_gridview2.R;
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 HeadAdapter extends BaseAdapter {
private Context context;
private List<Map<String,Object>> datas;
public HeadAdapter(Context context, List<Map<String, Object>> datas) {
super();
this.context = context;
this.datas = datas;
}
@Override
public int getCount() {
int count = 0;
if(datas!=null)
count = datas.size();
return count;
}
@Override
public Object getItem(int position) {
return datas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView==null)
{
convertView = LayoutInflater.from(context).inflate(R.layout.item_head, null);
holder = new ViewHolder();
holder.imageHead = (ImageView) convertView.findViewById(R.id.imageView);
holder.textInfo = (TextView) convertView.findViewById(R.id.textView);
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
Map<String,Object> data = datas.get(position);
holder.imageHead.setImageResource(Integer.parseInt(data.get("headImage").toString()));
holder.textInfo.setText(data.get("headInfo").toString());
return convertView;
}
class ViewHolder
{
ImageView imageHead;
TextView textInfo;
}
}
目标界面
package com.example.gp11_day12_gridview2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.gp11_day12_gridview2.adatper.HeadAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class HeadActivity extends Activity {
private GridView gridView;
private List<Map<String,Object>> datas;
private HeadAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_head);
gridView = (GridView) findViewById(R.id.gridView);
datas = new ArrayList<Map<String,Object>>();
int[] heads={R.drawable.img01,R.drawable.img02,R.drawable.img03,
R.drawable.img04,R.drawable.img05,R.drawable.img06,
R.drawable.img07,R.drawable.img08,R.drawable.img09};
Map<String,Object> map = null;
for(int i=0;i<heads.length;i++)
{
map = new HashMap<String,Object>();
map.put("headImage", heads[i]);
map.put("headInfo", "头像"+(i+1));
datas.add(map);
}
adapter = new HeadAdapter(this,datas);
gridView.setAdapter(adapter);
//给GridView添加事件监听
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//得到被点击的数据项
Map<String,Object> map = datas.get(position);
int imageId = Integer.parseInt(map.get("headImage").toString());
//把图片的资源id返回给上一个activity
Intent content = new Intent();
content.putExtra("imageId", imageId);
setResult(RESULT_OK,content);
finish();
}
});
}
}
源界面
package com.example.gp11_day12_gridview2;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static final int REQUESTCODE = 6;
private ImageView imageHead;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageHead = (ImageView) findViewById(R.id.image_head);
}
//点击按钮时选择头像
public void selectHead(View v)
{
Intent intent = new Intent(this,HeadActivity.class);
startActivityForResult(intent, REQUESTCODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==this.REQUESTCODE && resultCode==this.RESULT_OK)
{
int imageid = data.getIntExtra("imageId", R.drawable.ic_launcher);
imageHead.setImageResource(imageid);
}
}
}