为了防止当数据过多时,出现错误,利用一个convertview对View进行缓存,可以给下一个重新使用。
先定义一个Student类
package StudentAdapter;
import android.widget.ImageView;
/**
* Created by Administrator on 2015/8/24.
*/
public class Student {
private String name;
private String age;
private String sex;
private String hobit;
private int image;
public Student(String name,String sex,String age,String hobit,int image){
this.name=name;
this.age=age;
this.sex=sex;
this.hobit=hobit;
this.image=image;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getName() {
return name;
}
public String getHobit() {
return hobit;
}
public String getSex() {
return sex;
}
public String getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(String age) {
this.age = age;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setHobit(String hobit) {
this.hobit = hobit;
}
}
定义一个StudentAdapter的适配器,添加convertview
package StudentAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.administrator.myapplication.R;
import StudentAdapter.StudentAdapter.ViewHolder;
import java.util.HashMap;
import java.util.List;
/**
* Created by Administrator on 2015/8/24.
*/
public class StudentAdapter extends BaseAdapter{
private List mData;
private LayoutInflater mInflater;
private int count;
public StudentAdapter(LayoutInflater Inflater,List data){
//将Inflater和数据传递到
mInflater=Inflater;
mData=data;
}
@Override
//
public int getCount() {
count=mData.size();
return mData.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Student student =mData.get(position);
// 添加了防止多条数据出错的机制
if (convertView==null){
convertView=mInflater.inflate(R.layout.acativity_simpleadapter,null);
}
ImageView imageView= (ImageView) convertView.findViewById(R.id.image);
TextView textview_name= (TextView) convertView.findViewById(R.id.textview_name);
TextView textview_age= (TextView) convertView.findViewById(R.id.textview_age);
TextView textview_sex= (TextView) convertView.findViewById(R.id.textview_sex);
TextView textview_hobit= (TextView) convertView.findViewById(R.id.textview_hobit);
textview_name.setText(student.getName());
textview_age.setText(student.getAge());
textview_sex.setText(student.getSex());
textview_hobit.setText(student.getHobit());
imageView.setImageResource(student.getImage());
return convertView;
}
因为每次使用convertview时都要对findViewbyId进行一次实例,为了减少内存的占用,自定义一个viewHolder方法
package StudentAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.administrator.myapplication.R;
import StudentAdapter.StudentAdapter.ViewHolder;
import java.util.HashMap;
import java.util.List;
/**
* Created by Administrator on 2015/8/24.
*/
public class StudentAdapter extends BaseAdapter{
private List mData;
private LayoutInflater mInflater;
private int count;
public StudentAdapter(LayoutInflater Inflater,List data){
//将Inflater和数据传递到
mInflater=Inflater;
mData=data;
}
@Override
//
public int getCount() {
count=mData.size();
return mData.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Student student =mData.get(position);
//为了减少内存的使用
ViewHolder vh=null;
if (convertView==null) {
vh=new ViewHolder();
convertView=mInflater.inflate(R.layout.acativity_simpleadapter,null);
vh.imageView= (ImageView) convertView.findViewById(R.id.image);
vh.textview_name= (TextView) convertView.findViewById(R.id.textview_name);
vh.textview_age= (TextView) convertView.findViewById(R.id.textview_age);
vh.textview_sex= (TextView) convertView.findViewById(R.id.textview_sex);
vh.textview_hobit= (TextView) convertView.findViewById(R.id.textview_hobit);
convertView.setTag(vh);
Log.d("convertView","convertView为空时加载"+count);
}else{
vh= (ViewHolder) convertView.getTag();
Log.d("convertView","convertView不为空时加载"+count);
}
count++;
vh.textview_name.setText(student.getName());
vh.textview_age.setText(student.getAge());
vh.textview_sex.setText(student.getSex());
vh.textview_hobit.setText(student.getHobit());
vh.imageView.setImageResource(student.getImage());
return convertView;
}
class ViewHolder{
ImageView imageView;
TextView textview_name;
TextView textview_age;
TextView textview_sex;
TextView textview_hobit;
}
}
AdapterViewActivity类中
package com.example.administrator.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import StudentAdapter.Student;
import StudentAdapter.StudentAdapter;
/**
* Created by Administrator on 2015/8/24.
*/
public class AdpaterViewActivity extends Activity {
private ListView listView_name;
private List mDataStudent;
private String []array={"张三","李四","王五","赵六"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adapterview);
listView_name= (ListView) findViewById(R.id.listview_adapterview);
LayoutInflater inflater=getLayoutInflater();
setData();
StudentAdapter studentAdapter=new StudentAdapter(inflater,mDataStudent);
Log.d("Null point",""+listView_name+"11111");
listView_name.setAdapter(studentAdapter);
}
private void setData() {
mDataStudent=new ArrayList<>();
Student zhangsan=new Student("张三", "男", "18", "打球",R.mipmap.jie1);
Student lisi=new Student("李四","男","21","爱吃",R.mipmap.image_boy);
Student wangwu=new Student("王五","女","19","爱喝",R.mipmap.jie7);
Student zhaoliu=new Student("赵六","女","20","爱玩",R.mipmap.image_girl);
for (int i=0;i<50;i++){
mDataStudent.add(zhangsan);
mDataStudent.add(lisi);
mDataStudent.add(wangwu);
mDataStudent.add(zhaoliu);
}
}
}
android:cacheColorHint=”#00000000”设置listview背景的颜色
android:divider=”@color/treegreen”设置LisView的数据间的间隔的颜色
android:dividerHeight=”2dp”设置ListView的数据间的间隔距离
activity_adapterview.xml(同时也是上面的convertView布局)
"http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/image_bac_green">
"@+id/listview_adapterview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:divider="@color/treegreen"
android:dividerHeight="2dp">
activity_simpleadapter.xml
"http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/adapter_bacground">
"@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
"wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
"@+id/textview_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
"wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
"@+id/textview_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
"@+id/textview_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
"@+id/textview_hobit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
adapter_bacground.xml
"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android">
- "@mipmap/imagegreen"
android:state_pressed="true"
/>
在activity_fruit_adapter中添加checkBox
在ListView中设置屏蔽子控件抢夺ListView的焦点,在ListView中添加
android:descendantFocusability="blocksDescendants"
在子控件本身设置焦点属性为false
android:focusable="false"
>
activity_fruit_adapter
"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
"@+id/checkbox_fruit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:text="选择的水果是"/>
"@+id/image_fruit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/apple"/>
"@+id/text_fruit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="苹果"/>
activity_fruit.xml
"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/image_bac_green">
"@+id/listview_fruit"
android:layout_width="match_parent"
android:layout_height="match_parent">
Fruit.java
package StudentAdapter;
/**
* Created by Administrator on 2015/8/25.
*/
public class Fruit {
private int img;
private String fruit_name;
private boolean isCheck;
public Fruit( String fruit_name,int img) {
this.img = img;
this.fruit_name = fruit_name;
}
public boolean isCheck() {
return isCheck;
}
public void setIsCheck(boolean isCheck) {
this.isCheck = isCheck;
}
public int getImg() {
return img;
}
public void setImg(int img) {
this.img = img;
}
public String getFruit_name() {
return fruit_name;
}
public void setFruit_name(String fruit_name) {
this.fruit_name = fruit_name;
}
}
添加一个Frutiadapter适配器FruitAdapter.java
package StudentAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.administrator.myapplication.R;
import java.util.List;
/**
* Created by Administrator on 2015/8/25.
*/
public class FruitAdapter extends BaseAdapter{
private List mData;
private LayoutInflater mInflater;
private int count;
private boolean []mMangerAllCheckBox;
public FruitAdapter( LayoutInflater mInflater,List mData) {
this.mData = mData;
this.mInflater = mInflater;
mMangerAllCheckBox=new boolean[mData.size()];
}
public void checkedAll(){
for (int i=0;itrue;
}
notifyDataSetChanged();
}
public void contraryChecked(){
for (int i=0;ipublic void itemSelect(int position){
mMangerAllCheckBox[position]=!mMangerAllCheckBox[position];
notifyDataSetChanged();
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Fruit fruit =mData.get(position);
ViewHolder vh=null;
if (convertView==null) {
vh=new ViewHolder();
convertView=mInflater.inflate(R.layout.activity_fruit_adapter,null);
vh.checkBox_fruit= (CheckBox) convertView.findViewById(R.id.checkbox_fruit);
vh.imageView= (ImageView) convertView.findViewById(R.id.image_fruit);
vh.textview_name= (TextView) convertView.findViewById(R.id.text_fruit);
convertView.setTag(vh);
}else{
vh= (ViewHolder) convertView.getTag();
}
count++;
vh.textview_name.setText(fruit.getFruit_name());
vh.imageView.setImageResource(fruit.getImg());
vh.checkBox_fruit.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d("myfruit","选择框的变化为:"+isChecked);
mData.get(position).setIsCheck(isChecked);
notifyDataSetChanged();
}
});
vh.checkBox_fruit.setChecked(mMangerAllCheckBox[position]);
return convertView;
}
class ViewHolder{
ImageView imageView;
TextView textview_name;
CheckBox checkBox_fruit;
}
}
FruitAdapterAvitvity.java
package com.example.administrator.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import StudentAdapter.Fruit;
import StudentAdapter.FruitAdapter;
/**
* Created by Administrator on 2015/8/25.
*/
public class FruitAdapterActivity extends Activity{
private ListView listView_name;
private List mDataFruit;
private FruitAdapter fruitAdapter;
private Button button_checkAll;
private Button button_contraryCheck;
private View headerView;
private View bottomView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fruit);
listView_name= (ListView) findViewById(R.id.listview_fruit);
LayoutInflater inflater=getLayoutInflater();
setData();
// final FruitAdapter fruitAdapter=new FruitAdapter(inflater,mDataFruit);
headerView=inflater.inflate(R.layout.activity_checkall,null);
button_checkAll= (Button) headerView.findViewById(R.id.button_checkAll);
button_checkAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fruitAdapter.checkedAll();
}
});
listView_name.addHeaderView(headerView);
bottomView=inflater.inflate(R.layout.activity_contrarycheck,null);
button_contraryCheck= (Button) bottomView.findViewById(R.id.button_contraryCheck);
button_contraryCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fruitAdapter.contraryChecked();
}
});
listView_name.addFooterView(bottomView);
fruitAdapter=new FruitAdapter(inflater,mDataFruit);
listView_name.setAdapter(fruitAdapter);
listView_name.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
Log.d("Myfruit","水果名称"+mDataFruit.get(position-1).getFruit_name());
fruitAdapter.itemSelect(position-1);
}
});
}
private void setData() {
mDataFruit = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Fruit apple = new Fruit("苹果", R.mipmap.apple);
Fruit pineapple = new Fruit("菠萝", R.mipmap.pineapple);
Fruit strawberry = new Fruit("草莓", R.mipmap.strawberry);
Fruit watermelon = new Fruit("西瓜", R.mipmap.fruit_ninja);
mDataFruit.add(apple);
mDataFruit.add(pineapple);
mDataFruit.add(strawberry);
mDataFruit.add(watermelon);
}
}
}
添加activity_checkall.xml和activity_contrarycheck.xml
"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">