1、繼承BaseAdapter
public class ListViewAdapter extends BaseAdapter
{
LayoutInflater inflater;
private Context context;
private String[] content;
private int count;
public ListViewAdapter(Context context, String[] content)
{
this.content = content;
this.context = context;
this.count = content.length;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount()
{
return count;
}
public void setCount(int count)
{
if (count > content.length)
this.count = content.length;
else if (count > 0)
this.count = count;
}
@Override
public Object getItem(int position)
{
return content[position];
}
@Override
public long getItemId(int arg0)
{
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
TextView txtView = null;
if (convertView == null)
convertView = inflater.inflate(R.layout.listitem, null);
txtView = (TextView) convertView.findViewById(R.id.listcontent);
txtView.setText(content[position]);
return convertView;
}
}
2、分部綁定,其實就是控制adapter的getCount()函數傳回的元素個數
//建立listView的擴充卡
final ListViewAdapter listAdapter = new ListViewAdapter(this, content);
if (content.length > countForInit)//分組數大于countForInit
{
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.listitem, null);
view.setBackgroundResource(R.drawable.down);
//add footer View
listView.addFooterView(view);
//設定初始顯示個數
listAdapter.setCount(countForInit);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
if (position == listAdapter.getCount())//判斷點選的是否是footer
{
//每次增加countForInit個需要顯示的元素
listAdapter.setCount(listAdapter.getCount() + countForInit);
//通知Adpter資料已改變
listAdapter.notifyDataSetChanged();
//所有分組已顯示完,移除footer
if (listAdapter.getCount() == content.length)
listView.removeFooterView(view);
}
}
});
public class ListViewAdapter extends BaseAdapter
{
private Context context;
private String[] content;
private String[] contentid;
private int count;
public ListViewAdapter(Context context, String[] content, String[] contentid)
{
this.content = content;
this.context = context;
this.contentid = contentid;
this.count = content.length;
}
@Override
public int getCount()
{
return count;
}
public void setCount(int count)
{
if (count > content.length)
this.count = content.length;
else if (count > 0)
this.count = count;
}
@Override
public Object getItem(int position)
{
return content[position];
}
} 3、繼承SimpleCursorAdapter(綁定圖檔)
SimpleCursorAdapter adapter = new PersonAdapter(this, R.layout.person_item, cursor, new String[] { "_id", "pname", "pgender" }, new int[] { R.id.tvPid, R.id.tvPname, R.id.ivPgender });
lvPerson.setAdapter(adapter);
class PersonAdapter extends SimpleCursorAdapter
{
private Cursor mCursor;
protected int[] mFrom;
protected int[] mTo;
private ViewBinder mViewBinder;
public PersonAdapter(Context context, int layout, Cursor c, String[] from, int[] to)
{
super(context, layout, c, from, to);
mCursor = c;
mTo = to;
findColumns(from);
}
@Override
public void bindView(View view, Context context, Cursor cursor)
{
final ViewBinder binder = mViewBinder;
final int count = mTo.length;
final int[] from = mFrom;
final int[] to = mTo;
for (int i = 0; i < count; i++)
{
final View v = view.findViewById(to[i]);
if (v != null)
{
boolean bound = false;
if (binder != null)
bound = binder.setViewValue(v, cursor, from[i]);
if (!bound)
{
String text = cursor.getString(from[i]);
if (text == null)
text = "";
if (v instanceof TextView)
setViewText((TextView) v, text);
else if (v instanceof ImageView)
{
if (text.equals("男"))
setViewImage((ImageView) v, String.valueOf(R.drawable.boy));
else
setViewImage((ImageView) v, String.valueOf(R.drawable.girl));
}
else
throw new IllegalStateException(v.getClass().getName() + " is not a " + " view that can be bounds by this SimpleCursorAdapter");
}
}
}
}
private void findColumns(String[] from)
{
if (mCursor != null)
{
int i;
int count = from.length;
if (mFrom == null || mFrom.length != count)
mFrom = new int[count];
for (i = 0; i < count; i++)
mFrom[i] = mCursor.getColumnIndexOrThrow(from[i]);
}
else
mFrom = null;
}
}
4、繼承BaseAdapter,實作綁定圖檔等控件
public class ViewHolderAdapter extends BaseAdapter
{
private LayoutInflater mInflater;
private List<Map<String, Object>> mData;
public static Map<Integer, Boolean> isSelected;
public ViewHolderAdapter (Context context)
{
mInflater = LayoutInflater.from(context);
init();
}
// 初始化
private void init()
{
mData = new ArrayList<Map<String, Object>>();
for (int i = 0; i < 5; i++)
{
Map<String, Object> map = new HashMap<String, Object>();
map.put("img", R.drawable.icon);
map.put("title", "第" + (i + 1) + "行的标題");
mData.add(map);
}
// 這兒定義isSelected這個map是記錄每個listitem的狀态,初始狀态全部為false。
isSelected = new HashMap<Integer, Boolean>();
for (int i = 0; i < mData.size(); i++)
{
isSelected.put(i, false);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null; // convertView為null的時候初始化convertView。
if (convertView == null)
{
holder = new ViewHolder();
convertView = mInflater.inflate(1, null);
holder.img = (ImageView) convertView.findViewById(2);
holder.title = (TextView) convertView.findViewById(3);
holder.cBox = (CheckBox) convertView.findViewById(4);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.img.setBackgroundResource((Integer) mData.get(position).get("img"));
holder.title.setText(mData.get(position).get("title").toString());
holder.cBox.setChecked(isSelected.get(position));
return convertView;
}
public final class ViewHolder
{
public ImageView img;
public TextView title;
public CheckBox cBox;
}
}