ListView getView生成Item
2307 ワード
簡単です.
使用
public class GenerateItemViewOfGroup extends BaseAdapter
{
private Context mContext;
private List<String> listOfGroup;
private int sizeOfGroupList;
public GenerateItemViewOfGroup(Context context, List<String> listOfGroup)//
{
mContext = context;
this.listOfGroup = listOfGroup;
sizeOfGroupList = listOfGroup.size();
}
class ViewHolder
{
TextView tvGroupName;//
LinearLayout llGroupBg;//item
}
/**
*
*
* */
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder viewHolder;
if(convertView == null)// !!! !
{
LayoutInflater myInflater = LayoutInflater.from(mContext);
convertView = myInflater.inflate(R.layout.contact_group_list_item_default, null);//
viewHolder = new ViewHolder();
viewHolder.llGroupBg = (LinearLayout)convertView.findViewById(R.id.cglid_ll);
viewHolder.tvGroupName = (TextView)convertView.findViewById(R.id.cglid_tv_content);
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder)convertView.getTag();
}
String groupName = listOfGroup.get(position);
viewHolder.tvGroupName.setText(groupName);
return convertView;
}
/**
* Item , 0,
*/
public int getCount() {
// TODO Auto-generated method stub
return sizeOfGroupList;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
使用
List<String> listOfGroup = AddANewGroup.queryCurrentGroupInfo(this);//
GenerateItemViewOfGroup myItemView = new GenerateItemViewOfGroup(this, listOfGroup);
lvOfGroup.setAdapter(myItemView);