带checkBox的ListView小例子

闲来无事,写一个小例子,先上图看样子


先写上两个布局文件

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
	android:id="@+id/text"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Test ListView CheckBox"
    />
<ListView
	android:id="@+id/listview"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:cacheColorHint="#00000000"
></ListView>
</LinearLayout>

listitem.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    >
<TextView
	android:id="@+id/itemtext"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0"
    android:gravity="left"
    android:text="Test ListView CheckBox"
    />
<CheckBox
	android:id="@+id/itemcheck"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:checked="false"
></CheckBox>
</LinearLayout>


然后先写一个实体类

package cn.com.sligner.listCheckBox;

public class Student {
private String name;
private int age;
private int score;

public Student(String name, int age, int score) {
	super();
	this.name = name;
	this.age = age;
	this.score = score;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}
public int getScore() {
	return score;
}
public void setScore(int score) {
	this.score = score;
}
}

接着写Adapter类,其中会用到一个自定义接口

接口:

package cn.com.sligner.listCheckBox;

import android.widget.CompoundButton;

public interface OnItemCheckedChanged {
public void onItemCheckedChanged(CompoundButton view,int position, boolean isChecked);
}

adapter类

package cn.com.sligner.listCheckBox;

import java.util.ArrayList;

import android.content.Context;
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.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class ListAdapter extends BaseAdapter{
private Context mContext;
private ArrayList<Student> list;
private OnItemCheckedChanged onItemCheckedChangedListener;
public void setOnItemCheckedChangedListener(
		OnItemCheckedChanged onItemCheckedChangedListener) {
	this.onItemCheckedChangedListener = onItemCheckedChangedListener;
}
public ListAdapter(Context mContext, ArrayList<Student> list){
	super();
	this.mContext = mContext;
	this.list = list;
}
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.listitem, null, false);
		}
		TextView text = (TextView)convertView.findViewById(R.id.itemtext);
		text.setText(list.get(position).getName());
		CheckBox checkBox = (CheckBox)convertView.findViewById(R.id.itemcheck);
		checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(onItemCheckedChangedListener != null)
				onItemCheckedChangedListener.onItemCheckedChanged(buttonView, position, isChecked);
			}
		});
		return convertView;
	}

}


最后是测试Activity

package cn.com.sligner.listCheckBox;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.Toast;

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
ArrayList<Student> students = new ArrayList<Student>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        students.add(new Student("张三",20,60));
        students.add(new Student("李四",21,61));
        students.add(new Student("王五",22,62));
        students.add(new Student("赵六",23,63));
        students.add(new Student("钱七",24,64));
        students.add(new Student("周八",25,65));
        
        ListView listview = (ListView)this.findViewById(R.id.listview);
        ListAdapter listAdapter = new ListAdapter(this, students);
        listAdapter.setOnItemCheckedChangedListener(new OnItemCheckedChanged() {
			@Override
			public void onItemCheckedChanged(CompoundButton view, int position, boolean isChecked) {
				String yes = students.get(position).getName()+"选中了";
				String no = students.get(position).getName()+"取消了选中";
				Toast.makeText(TestActivity.this, isChecked ? yes : no , Toast.LENGTH_LONG).show();
			}
		});
        listview.setAdapter(listAdapter);
    }
}

第一次写博客,大家多提意见哈,多交流 大笑


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值