item.List.xml(自定义item布局样式)
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:ID="@+ID/activity_item_List" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:gravity="center_vertical" androID:orIEntation="horizontal" androID:padding="15dp" tools:context="com.example.jump.ItemListActivity"> <ImageVIEw androID:ID="@+ID/iv_img" androID:layout_wIDth="50dp" androID:layout_height="50dp" androID:src="@drawable/changan" androID:scaleType="centerCrop"/> <TextVIEw androID:ID="@+ID/name" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="长安" androID:layout_marginleft="15dp" androID:textSize="30sp"/></linearLayout>
新建实体类entity,描述自定义item布局中的属性,调用get和set方法
package com.example.entity;/** * Created by administrator on 2017/2/16. */public class CarCompany { private String company; private int img; public String getCompany() { return company; } public int getimg() { return img; } public voID setimg(int img) { this.img = img; } public voID setCompany(String company) { this.company = company; }}
在activity中实例化ListvIEw控件,创建数据源(构建演示数据 List<?>)
package com.example.jump;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.Widget.ArrayAdapter;import androID.Widget.ListVIEw;import com.example.adapter.MyAdapter;import com.example.entity.CarCompany;import java.util.ArrayList;import java.util.List;public class ArrayAdapteractivity extends AppCompatActivity { private ListVIEw lv; private List<String> str_List; private String[] names=new String[]{"长安", "沃尔沃", "大众", "宾利", "奔驰"}; private int[] IDs=new int[]{R.drawable.changan,R.drawable.vol,R.drawable.das,R.drawable.bl,R.drawable.benz}; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_array_adapter); lv= (ListVIEw) findVIEwByID(R.ID.lv_List);// intiArrayList(); intiCustomAdapter(); } private voID intiArrayList(){ //数据构建完成 str_List=new ArrayList<String>(); str_List.add("AndroID"); str_List.add("IOS"); str_List.add("windowsPhone"); //Adapter ArrayAdapter adapter=new ArrayAdapter(this,androID.R.layout.simple_List_item_1,str_List); //将适配器设置给ListvIEw lv.setAdapter(adapter); } //新建List存放carcompany数据 private voID intiCustomAdapter(){ List<CarCompany> List=new ArrayList<CarCompany>(); for (int i = 0; i < names.length; i++) { CarCompany company=new CarCompany(); company.setimg(IDs[i]); company.setCompany(names[i]); List.add(company); } MyAdapter myadapter=new MyAdapter(List,this); lv.setAdapter(myadapter); }}
新建Adapter(自定义)
package com.example.adapter;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 com.example.entity.CarCompany;import com.example.jump.R;import java.util.List;/** * Created by administrator on 2017/2/16. */public class MyAdapter extends BaseAdapter { private List<CarCompany> List;//数据源 private Context context;//上下文对象 private LayoutInflater inflater;//布局加载器 //有参的构造函数,为数据源,上下文对象复制,同时实例化布局加载器 public MyAdapter(List<CarCompany> List,Context context) { this.List=List; this.context=context; inflater=LayoutInflater.from(context); } //有多少条数据,需要创建多少个item布局 @OverrIDe public int getCount() { return List.size(); } //返回position对应位置的数据 @OverrIDe public Object getItem(int position) { return List.get(position); } //返回position对应位置item的ID @OverrIDe public long getItemID(int position) { return position; } /** * 具体定义加载item布局,并将数据显示到item布局上的方法。 * @param position * @param convertVIEw * @param parent * @return */ @OverrIDe public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) { //加载item布局 将xml布局加载到内存中,形成一个vIEw VIEw vIEw=inflater.inflate(R.layout.activity_item_List,null); //实例化item布局上的控件 ImageVIEw iv= (ImageVIEw) vIEw.findVIEwByID(R.ID.iv_img); TextVIEw tv= (TextVIEw) vIEw.findVIEwByID(R.ID.name); //往控件上显示数据 //获取position对应位置的数据 CarCompany company= (CarCompany) getItem(position); iv.setimageResource(company.getimg()); tv.setText(company.getCompany()); return vIEw; }}
在activity中实例化adapter,设置给ListvIEw
/新建List存放carcompany数据 private voID intiCustomAdapter(){ List<CarCompany> List=new ArrayList<CarCompany>(); for (int i = 0; i < names.length; i++) { CarCompany company=new CarCompany(); company.setimg(IDs[i]); company.setCompany(names[i]); List.add(company); } MyAdapter myadapter=new MyAdapter(List,this); lv.setAdapter(myadapter); }
总结 以上是内存溢出为你收集整理的自定义Adapter与系统自带的adapter区别(样例步骤).txt全部内容,希望文章能够帮你解决自定义Adapter与系统自带的adapter区别(样例步骤).txt所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)