自定义Adapter

先创建一个实体类,用于存放数据

package com.example.zhujunxian.zhujunxian;

public class Curtom {

    private int icon;
    private String nick;

    public Curtom(int icon, String nick) {
        this.icon = icon;
        this.nick = nick;
    }

    public int getIcon() {
        return icon;
    }

    public void setIcon(int icon) {
        this.icon = icon;
    }

    public String getNick() {
        return nick;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }
}

在此Activity中设置适配器

package com.example.zhujunxian.zhujunxian;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

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

public class CurtomAdapterActivity extends Activity {

    private final static String TAG = "zhujunxiana";
    private ListView listView;
    private CurtomMyadapter myadapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.curtom);
        //Log.i(TAG,"test");
        listView = (ListView) findViewById(R.id.listviewone);
        List<Curtom> list = new ArrayList<>(10);

        int a1 = R.drawable.a;
        Curtom curtom1 = new Curtom(a1,"zhujunxian");
        list.add(curtom1);
        int a2 = R.drawable.a;
        Curtom curtom2 = new Curtom(a2,"zhujunxian");
        list.add(curtom2);
        int a3 = R.drawable.a;
        Curtom curtom3 = new Curtom(a3,"zhujunxian");
        list.add(curtom3);
        int a4 = R.drawable.a;
        Curtom curtom4 = new Curtom(a4,"zhujunxian");
        list.add(curtom4);
        int a5 = R.drawable.a;
        Curtom curtom5 = new Curtom(a5,"zhujunxian");
        list.add(curtom5);

        int a6 = R.drawable.a;
        Curtom curtom6 = new Curtom(a6,"zhujunxian");
        list.add(curtom6);
        int a7 = R.drawable.a;
        Curtom curtom7 = new Curtom(a7,"zhujunxian");
        list.add(curtom7);
        int a8 = R.drawable.a;
        Curtom curtom8 = new Curtom(a8,"zhujunxian");
        list.add(curtom8);
        int a9 = R.drawable.a;
        Curtom curtom9 = new Curtom(a9,"zhujunxian");
        list.add(curtom9);
        int a10 = R.drawable.a;
        Curtom curtom10 = new Curtom(a10,"zhujunxian");
        list.add(curtom10);

        
        myadapter = new CurtomMyadapter(CurtomAdapterActivity.this,list);
        listView.setAdapter(myadapter);

    }

}

创建适配器

package com.example.zhujunxian.zhujunxian;

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;

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

public class CurtomMyadapter extends BaseAdapter {
    private List list;
    //布局加载器,将Xml文件转化成RelativeLayout
    private LayoutInflater mInflater;

    public CurtomMyadapter(Context context, List<Curtom> list) {
        this.list = list;
        //初始化布局加载器
        mInflater = LayoutInflater.from(context);
    }

    public CurtomMyadapter(CurtomAdapterActivity curtomAdapterActivity, int itme, String[] arraylist) {
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //每个Item的效果;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //加载每个item的布局对象,(将itme转为RelativeLayout对象) 
      // View view = mInflater.inflate(R.layout.itme, null);
      //这是判断每个item如果被加载过之后,就不必重复加载,上面那种会重复在家,性能不好。
      View view;
       ViewHolder viewHolder;
      if(convertView == null){
      view = mInflater.inflate(R.layout.itme, null);
      //添加ViewHolder
       viewHolder = new ViewHolder();
        viewHolder.imageView = (ImageView) view.findViewById(R.id.imageViewone);
        viewHolder.textView = (TextView) view.findViewById(R.id.textViewone);
       //将ImageView,TextView控件保存在ViewHolder中
        view.setTag(viewHolder);
      }else{
      view = convertView;
       // 重新获取ViewHolder
       viewHolder = (ViewHolder) view.getTag();
      }
        //获取数据显示内容
        Curtom curtom = (Curtom) list.get(position);
        imageView.setImageResource(curtom.getIcon());
        textView.setText(curtom.getNick());
        return view;
    }
    class ViewHolder { 
         ImageView imageView;
         TextView textView;
       }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值