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);
}