android自定义ListView
2010-01-22 11:32
public class PeoplesAdapter extends BaseAdapter {
private Context context;
private LinearLayout peopleLineLayout;
private EditText headTxtSearch;
public PeoplesAdapter(Context c) {
this.context = c;
mInflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
private ArrayList<PeopleVO> peoples = null;
private LayoutInflater mInflater = null;
@Override
public int getCount() {
if (peoples != null) {
return peoples.size();
} else {
return 0;
}
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
private static class ViewHolder {
ImageView imgCheck;
TextView txtName;
TextView txtMobile;
TableLayout peopleTableLayout;
PeopleVO people;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.list_people, null);
holder.imgCheck = (ImageView) convertView
.findViewById(R.id.imgCheck);
holder.txtName = (TextView) convertView
.findViewById(R.id.txtPeopleName);
holder.txtMobile = (TextView) convertView
.findViewById(R.id.txtPeopleMobile);
} else {
holder = (ViewHolder) convertView.getTag();
}
PeopleVO currentPeople = peoples.get(position);
holder.txtName.setText(currentPeople.name);
holder.txtMobile.setText(currentPeople.mobile);
holder.people = currentPeople;
convertView.setTag(holder);
convertView.setOnClickListener(viewOnClick);
return convertView;
}
View.OnClickListener viewOnClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
v.setSelected(true);
ImageView imgView = (ImageView) v.findViewById(R.id.imgCheck);
ViewHolder holder = (ViewHolder) v.getTag();
PeopleVO people = holder.people;
if (people.isSelect) {
imgView.setImageResource(R.drawable.checkbox_off_background);
} else {
imgView.setImageResource(R.drawable.checkbox_on_background);
}
people.isSelect = !people.isSelect;
}
};
public ArrayList<PeopleVO> getPeoples() {
return peoples;
}
public void setPeoples(ArrayList<PeopleVO> peoples) {
this.peoples = peoples;
notifyDataSetChanged();
}
public void addPeople(PeopleVO people) {
if (peoples == null) {
peoples = new ArrayList();
}
peoples.add(people);
}
Activity 继承 ListActivity
在onCreate中设置Adapter
setListAdapter(new PeoplesAdapter (this));
list_people.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">
<ImageView android:layout_width="wrap_content"
android:layout_height="fill_parent" android:id="@+id/imgCheck" android:src="@drawable/checkbox_off_background" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="8">
<TextView android:id="@+id/txtPeopleName"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="32px" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="手机:" />
<TextView android:layout_width="fill_parent" android:id="@+id/txtPeopleMobile"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
效果如下: