自定义BaseAdapter

本文介绍了一种通过自定义BaseAdapter子类来简化Android应用中ListView数据绑定的方法。该适配器提供了添加、更新和清除数据项的功能,并支持单条或多条数据的添加操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义一个类继承自BaseAdapter,将一些Adapter中相同的方法封装到自定义类中,在写Adapter的时候直接继承自这个自定义类

 

public abstract classMyBaseAdapter<T> extends BaseAdapter {

       protectedContext context;

       protectedLayoutInflater inflater;

       protectedArrayList<T> myList=new ArrayList<T>();

      

       publicMyBaseAdapter(Context context){

              this.context=context;

              this.inflater=LayoutInflater.from(context);

       }

       //封装加数据的方法--1条 多条    

       publicvoid appendData(T t,boolean isClearOld){

              if(t==null)

                     return;

              if(isClearOld)

                     myList.clear();

              myList.add(t);

       }

       publicvoid appendData(ArrayList<T> data,boolean isClearOld){

              if(data==null)

                     return;

              if(isClearOld)

                     myList.clear();

              myList.addAll(data);

       }

       //封装加数据的方法--1条 多条    在顶部添加

       publicvoid appendDataTop(T t,boolean isClearOld){

              if(t==null)

                     return;

              if(isClearOld)

                     myList.clear();

              myList.add(0,t);

       }

       publicvoid appendDataTop(ArrayList<T> data,boolean isClearOld){

              if(data==null)

                     return;

              if(isClearOld)

                     myList.clear();

              myList.addAll(0,data);

       }

      

       publicvoid update(){

              this.notifyDataSetChanged();

       }

      

       publicvoid clear(){

              myList.clear();

       }

      

 

       publicint getCount() {

              if(myList==null)

                     return0;

              returnmyList.size();

       }

 

       publicT getItem(int position) {

              if(myList==null)

                     returnnull;

              if(position>myList.size()-1)

                     returnnull;

              returnmyList.get(position);

       }

 

       publiclong getItemId(int position) {

              returnposition;

       }

 

       publicView getView(int position, View convertView, ViewGroup parent) {

              returngetMyView(position, convertView, parent);

       }

       publicabstract View getMyView(int position, View convertView, ViewGroup parent);

      

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值