/* * Copyright (C) 2009 Teleca Poland Sp. z o.o. <[email protected]> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package grimbo.android.demo.adapter; import java.util.ArrayList; import android.app.Activity; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; /** * Nice wrapper-abstraction around ArrayList * * @author Lukasz Wisniewski * * @param <T> */ public abstract class ArrayListAdapter<T> extends BaseAdapter{ protected ArrayList<T> mList; protected Activity mContext; protected ListView mListView; public ArrayListAdapter(Activity context){ this.mContext = context; } @Override public int getCount() { if(mList != null) return mList.size(); else return 0; } @Override public Object getItem(int position) { return mList == null ? null : mList.get(position); } @Override public long getItemId(int position) { return position; } @Override abstract public View getView(int position, View convertView, ViewGroup parent); public void setList(ArrayList<T> list){ this.mList = list; notifyDataSetChanged(); } public ArrayList<T> getList(){ return mList; } public void setList(T[] list){ ArrayList<T> arrayList = new ArrayList<T>(list.length); for (T t : list) { arrayList.add(t); } setList(arrayList); } public ListView getListView(){ return mListView; } public void setListView(ListView listView){ mListView = listView; } }
使用方法
1、自定义一个adapter
public class PurpleAdapter extends ArrayListAdapter<PurpleEntry>
2、
//存入一个自定义泛型 private ArrayList<PurpleEntry> browseListEntry; mBrowseJamendoPurpleAdapter.setList(browseListEntry);
3、在自定义PurpleAdapter 中使用
if(mList.get(position).getText() != null){ holder.text.setText(mList.get(position).getText());