为了账号安全,请及时绑定邮箱和手机立即绑定

BaseAdapter适配器使用

标签:
Android

主要类

package com.example.shipeizia;

import java.util.ArrayList;
import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        List<itembean> itembeans = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            itembeans.add(new itembean(R.drawable.ic_launcher,"我是标题"+i, "我是内容,我是内容"+i));

        }

        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(new shipeiqi(this,itembeans));

    }

}

封装类

package com.example.shipeizia;

public class itembean {
      public int tupianid;
      public String biaotiString;
      public String neirongString;

      public itembean(int tupianid,String biaotiString,String neirongString){
           this.neirongString= neirongString;
           this.tupianid= tupianid;
           this.biaotiString=biaotiString;

      }
}

适配器

package com.example.shipeizia;

import java.util.List;
import java.util.zip.Inflater;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class shipeiqi extends BaseAdapter {

    public List<itembean> mlist;
    public LayoutInflater mInflater;

    public shipeiqi(Context context, List<itembean> list){
         mlist=list;//数据源和数据适配器关联
         mInflater = LayoutInflater.from(context);//context要是用当前的适配器界面对象mInflater布局装载器对象
    }

    @Override
    public int getCount() {
        // 返回ListView 显示的数据量
        return mlist.size();
    }

    @Override
    public Object getItem(int arg0) {
        // 制定索引和对应的数据项
        return mlist.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // 返回制定索引对应的数据项
        return arg0;
    }

    @Override
    public View getView(int a, View b, ViewGroup c) {
        ViewHolder viewHolder=null ;
        if (b==null) {
            viewHolder = new ViewHolder();
            b=mInflater.inflate(R.layout.item, null);
            viewHolder.imageView=(ImageView) b.findViewById(R.id.tupian);
            viewHolder.biaoti=(TextView) b.findViewById(R.id.biaoti);
            viewHolder.neirong=(TextView) b.findViewById(R.id.neirong);
            b.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) b.getTag();
        }

        itembean bean = mlist.get(a);
        viewHolder.imageView.setImageResource(bean.tupianid);
        viewHolder.biaoti.setText(bean.biaotiString);
        viewHolder.neirong.setText(bean.neirongString);
        return b;
    }

    class ViewHolder{
        public ImageView imageView;
        public TextView biaoti;
        public TextView neirong;
    }

}
点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消