二、使用simpleadapter(自定义adapter,同一列具有图片、文字、按钮)
①SimpleAdapterActivity.java
package com.week4.simpleadapter;
import java.util.ArrayList;
import java.util.HashMap;
import com.anjoyo.as.chap04.exam02.R;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.app.ListActivity;
public class SimpleAdapterActivity extends ListActivity {
//本身就是一个list,所以onCreate里不需要setContentView(因为继承自ListActivity)
/* 数据源:一个存放多个Map的ArrayList */
private ArrayList<HashMap<String, Object>> dataList; //数据源
HashMap<String, Object> map; //数据源的内容
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.array_adapter);
//准备好v
//2.准备数据源
dataList = new ArrayList<HashMap<String, Object>>(); //数据源初始化(分配空间)
/* 生成一个50项的数据源 */
for (int i = 1; i <= 50; i++) {
map = new HashMap<String, Object>(); //对每个数据源的内容初始化,即分配空间
//为map填充内容
map.put("img", R.drawable.ic_launcher);
map.put("text", "第" + i + "项文本信息");
map.put("cbx", ""); /* CheckBox后显示的文本信息,此时为空*/
dataList.add(map); //将map内容添加进数据源中
}
//3.实例化SimpleAdapter(this指上下文【此页面】,数据源,数据源要插入的页面,把img插入到img控件、text与cbx类似、不能对应错)
SimpleAdapter adapter = new SimpleAdapter(this, dataList,R.layout.simple_adapter_item,
new String[] {"img", "text", "cbx" },
new int[] {R.id.img, R.id.text, R.id.cbx });
//4.真正进行数据适配并加载;
//由于使用了ListActivity,所以可以直接使用Activity本身的setListAdapter方法
this.setListAdapter(adapter);
}
//处理用户点击选择事件,如果列表项中有可点击的控件,那么必须将这些控件设为不能获得焦点。(Android:focusable=“false”和And=“false”roid:focusableTouchmode==“false”)
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Toast.makeText(getApplicationContext(), "第"+(position+1)+"项被点击!", 1).show();
}
}
②simple_adapter_item.xml:(List每个选项的布局,不同的内容插入到不同的控件之中)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:contentDescription="@drawable/ic_launcher" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/img"
android:textColor="#00ff00"
android:textSize="18sp" />
<CheckBox
android:id="@+id/cbx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/text"
android:focusable="false"
android:focusableInTouchMode="false" />
RelativeLayout>
三、完全自定义adapter(只有一张图片)
①adsfadsdf.java(一个举例、模板)
package com.anjoyo.as.chap04.exam03;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public class adsfadsdf extends BaseAdapter { //自定义adapter(继承BaseAdapter需要实现以下方法)
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0; //网格布局的数量
}
@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
return null; //网格布局所对应的的页面,position:页面选项所对应的位置;convertview:网 格布局所对应的的页面
}
}
②CustomAdapterActivity .java
package com.anjoyo.as.chap04.exam03;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import android.app.Activity;
public class CustomAdapterActivity extends Activity {
GridView gridview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_adapter); //加载网格布局
gridview=(GridView) findViewById(R.id.gridview);
//将自定义的Adapter实例化,初始化
ImageAdapter imageAdapter=new ImageAdapter(this);//(实现上下文的传递,即把此页面传递给 adapter)
//真正完成数据适配并显示。(把适配器加载到网格布局中并实现adapter的其它方法)
gridview.setAdapter(imageAdapter);
}
}
③ImageAdapter.java
package com.anjoyo.as.chap04.exam03;
import android.content.Context;
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 { //自定义adapter
//准备数据源,这是一组要显示在GridView中一组图片
private Integer[] datalist= {R.drawable.def_0,R.drawable.def_1,R.drawable.def_2,
R.drawable.def_3,R.drawable.def_4,R.drawable.def_5,R.drawable.def_6,
R.drawable.def_7,R.drawable.def_0,R.drawable.def_1,R.drawable.def_2,
R.drawable.def_3,R.drawable.def_4,R.drawable.def_5,R.drawable.def_6,
R.drawable.def_7,R.drawable.def_0,R.drawable.def_1,R.drawable.def_2,
R.drawable.def_3,R.drawable.def_4,R.drawable.def_5,R.drawable.def_6,
R.drawable.def_7,R.drawable.def_0,R.drawable.def_1,R.drawable.def_2,
R.drawable.def_3,R.drawable.def_4,R.drawable.def_5,R.drawable.def_6,
R.drawable.def_7};
private Context context;
//构造方法,实现上下文的传递,实现类的初始化;
public ImageAdapter(Context c) {
context=c;
}
//返回填充的数据项的个数,必须返回真实
@Override
public int getCount() {
// TODO Auto-generated method stub
return datalist.length; //填充的个数即数据源的大小
}
//根据position返回某一项数据(在这个例子,返回的是一张图片)
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return datalist[position]; //获得第几个数据源所对应的选项
}
//根据position返回某一项的行ID
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
//getView是自定义Adapter里最重要的方法,根据position返回某一项的界面View,显示给用户。
//参数position,数据项的索引,从0开始;
//参数convertView,要重用的view,每次要检查他是否为null,如果不是,用直接重用它;如是null则新建一个view(缓存机制)
//参数parent,convertView所显示在的父view;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//1.准备v
ImageView img;
if(convertView==null)
{ //如是null则新建一个view(缓存机制)
img=new ImageView(context);
img.setLayoutParams(new GridView.LayoutParams(90,90));//图片框的大小;
img.setPadding(10, 10, 10, 10);//图片周围的间距;
img.setScaleType(ImageView.ScaleType.CENTER_CROP);
}else {
img=(ImageView)convertView;
}
//2.设置img的具体的图片(根据position读取到的),每次读取一张;
img.setImageResource(datalist[position]);
//3.img完成了准备工作,可以返回img给界面窗体;
return img;
}
}
④custom_adapter.xml:(网格布局)
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
三、完全自定义adapter(有文字、图片等)
①MainActivity.java:
package com.oracle.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;
import com.oracle.adapter.MyAdapter;
public class MainActivity extends Activity {
ListView lv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv1=(ListView) this.findViewById(R.id.lv1);
MyAdapter myadapter=new MyAdapter(); //初始化adapter
lv1.setAdapter(myadapter); //数据适配,并实现adapter的方法
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
②MyAdapter.java:
package com.oracle.adapter;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.oracle.activity.R;
import com.oracle.entity.User;
public class MyAdapter extends BaseAdapter {
//数据源list
List<User> list=new ArrayList<User>(); //数据源
public MyAdapter(){
//为数据源增添内容
User u1=new User("李健","<<贝加尔湖畔>>");
User u2=new User("韩红","<<莫尼山>>");
User u3=new User("王菲","<<传奇>>");
User u4=new User("阿杜","<<离别>>");
User u5=new User("德德玛","<<苍天般的阿拉善>>");
list.add(u1);
list.add(u2);
list.add(u3);
list.add(u4);
list.add(u5);
}
@Override
public int getCount() {
//返回适配的数据的总个数
return list.size();
}
@Override
public Object getItem(int position) {
//返回当前指定位置的一条数据
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//先得到一个布局加载器inflater
LayoutInflater inflater=(LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//再通过inflater去加载自定义的列表项布局
View view=inflater.inflate(R.layout.items,null);
TextView author=(TextView) view.findViewById(R.id.textView1);
author.setText(list.get(position).getAuthor()); //把数据源上的内容填充到选项上
TextView song=(TextView) view.findViewById(R.id.textView2);
song.setText(list.get(position).getSong());
return view;
}
}
③User.java:(封装类)
package com.oracle.entity;
public class User {
private String author;
private String song;
private String pic;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String author, String song, String pic) {
super();
this.author = author;
this.song = song;
this.pic = pic;
}
public User(String author, String song) {
super();
this.author = author;
this.song = song;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getSong() {
return song;
}
public void setSong(String song) {
this.song = song;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
}
④activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/lv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
ListView>
RelativeLayout>
⑤items.xml(每个选项的布局,不同的内容插入到不同的控件之中)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:id="@+id/textView1"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="TextView"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="TextView"
android:gravity="right"/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
LinearLayout>