public abstract View getView (int position, View convertView, ViewGroup parent)
Get a View that displays the data at the specified position in the data set. You can either create a View manually or inflate it from an XML layout file. When the View is inflated, the parent View
(GridView, ListView...) will apply default layout parameters unless you use inflate(int,
android.view.ViewGroup, boolean)
to specify a root view and to prevent attachment to the root.
Parameters
position | The position of the item within the adapter's data set of the item whose view we want. |
---|---|
convertView |
The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view. Heterogeneous lists can
specify their number of view types, so that this View is always of the right type (see getViewTypeCount() and getItemViewType(int) ). |
parent | The parent that this view will eventually be attached to |
Returns
- A View corresponding to the data at the specified position.
//ViewHolder靜態類
static class ViewHolder
{
public ImageView img;
public TextView title;
public TextView info;
}
public class MyAdapter extends BaseAdapter
{
private LayoutInflater mInflater = null;
private MyAdapter(Context context)
{
//根據context上下文加載佈局,這裏的是Demo17Activity本身,即this
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
//How many items are in the data set represented by this Adapter.
//在此適配器中所代表的數據集中的條目數
return data.size();
}
@Override
public Object getItem(int position) {
// Get the data item associated with the specified position in the data set.
//獲取數據集中與指定索引對應的數據項
return position;
}
@Override
public long getItemId(int position) {
//Get the row id associated with the specified position in the list.
//獲取在列表中與指定索引對應的行id
return position;
}
//Get a View that displays the data at the specified position in the data set.
//獲取一個在數據集中指定索引的視圖來顯示數據
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
//如果緩存convertView爲空,則需要創建View
if(convertView == null)
{
holder = new ViewHolder();
//根據自定義的Item佈局加載佈局
convertView = mInflater.inflate(R.layout.list_item, null);
holder.img = (ImageView)convertView.findViewById(R.id.img);
holder.title = (TextView)convertView.findViewById(R.id.tv);
holder.info = (TextView)convertView.findViewById(R.id.info);
//將設置好的佈局保存到緩存中,並將其設置在Tag裏,以便後面方便取出Tag
convertView.setTag(holder);
}else
{
holder = (ViewHolder)convertView.getTag();
}
holder.img.setBackgroundResource((Integer)data.get(position).get("img"));
holder.title.setText((String)data.get(position).get("title"));
holder.info.setText((String)data.get(position).get("info"));
return convertView;
}
}
convertView = mInflater.inflate(R.layout.list_item, null);
我將這個LayoutInflater public View getView(int position, View convertView, ViewGroup arg2) {
if (subject_list == null) {
return null;
}
if(convertView == null){
//首次加載默認控件
convertView = new OneView(mContext);
}
OneView newView = (OneSubjectView)convertView;
newView.updateView(subject_list.get(position)); //加載數據方法
convertView = newView;
return convertView;
}