- ArrayAdapter
public class MainActivity extends AppCompatActivity {
private ListView mListView;
//字符串组合
private String [] data = {"item_One","item_Two","item_Three","item_Four",
"item_Five","item_Six","item_Seven","item_Eight","item_Nine","item_Ten"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到listview 组件
mListView = (ListView) findViewById(R.id.lv_id);
/**
* 通过ArrayAdapter 将数据和布局联系起来
* 参数1 当前上下文环境
* 参数2 当前引用的布局 一般系统默认
* 参数3 当前绑定的数据
* */
ArrayAdapter<String> array = new ArrayAdapter<String>(
this,android.R.layout.simple_list_item_1,data);
//将数据和布局 显示到列表
mListView.setAdapter(array);
}
}
- SimpleAdapter
package com.example.lum.arrayadapterlistview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView mListView;
//字符串组合
private String [] data = {"item_One","item_Two","item_Three","item_Four",
"item_Five","item_Six","item_Seven","item_Eight","item_Nine","item_Ten"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到listview 组件
mListView = (ListView) findViewById(R.id.lv_id);
//创建一个含有hanshMap 的列表集合
List<HashMap<String,String>> data = new ArrayList<>();
for(int i = 1; i <=18; i++) { //创建 18 个 map 数据对象 ,每个map 对象 有两个键值数据
//创建HashMap 对象,添加键值数据
HashMap<String,String> map = new HashMap<>();
//向map 对象添加两组键值对数据
map.put("key_one","dataOne_" + i);
map.put("key_two","dataTwo_" + i);
//将 map 对象添加到data 集合
data.add(map);
}
/**
* SimpleAdapter 将数据和布局联系起来
* 参数1 当前上下文环境
* 参数2 当前绑定的数据
* 参数3 当前引用的listView 的列表的布局
* 参数4 String数组存储的map 的key
* 参数5 int 数组保存的 组件的id
*
* 参数4 ,参数5 值是对应关系
* */
SimpleAdapter mSimpleAdapter = new SimpleAdapter(
this,
data,
R.layout.listview_item,
new String[]{"key_one","key_two"},
new int[]{R.id.text_one,R.id.text_two});
//将数据通过适配器显示到lv
mListView.setAdapter(mSimpleAdapter);
}
}
- 自定义adapter
public class MyListAdapter extends BaseAdapter {
private List<StudentData> mStudentDataList; //创建一个StudentData 类的对象 集合
private LayoutInflater inflater;
public MyListAdapter (List<StudentData> mStudentDataList, Context context) {
this.mStudentDataList = mStudentDataList;
this.inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mStudentDataList == null?0:mStudentDataList.size(); //判断有说个Item
}
@Override
public Object getItem(int position) {
return mStudentDataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//加载布局为一个视图
View view = inflater.inflate(R.layout.listview_item,null);
StudentData mStudentData = (StudentData) getItem(position);
//在view 视图中查找 组件
TextView tv_name = (TextView) view.findViewById(R.id.text_name);
TextView tv_age = (TextView) view.findViewById(R.id.text_age);
ImageView im_photo = (ImageView) view.findViewById(R.id.phone_id);
//为Item 里面的组件设置相应的数据
tv_name.setText(mStudentData.getName());
tv_age.setText("age: "+ mStudentData.getAge());
im_photo.setImageResource(mStudentData.getPhoto());
//返回含有数据的view
return view;
}
}
- 优化getview方法
方法 public View getView(int position, View convertView, final ViewGroup parent) 有三个参数
第一个参数position---该视图在适配器数据中的位置
第二个参数convertView---旧视图:
展示在界面上的一个item。因为手机屏幕就那么大,所以一次展示给用户看见的内容是固定的,如果你List中有1000条数据,不应该new1000个
converView,那样内存肯定不足,应该学会控件重用,滑出屏幕的converView就在下面新进来的item中重新使用,只是修改下他展示的值
第三个参数parent: 此视图最终会被附加到的父级视图,这个属性是加载xml视图时使用,确定他的父控件,比如以下代码中用到parent
v = LayoutInflater.from(context).inflate(R.layout.adapter__item, parent, false);
复用converView(减少重复创建每个条目的view),使用ViewHolder(减少重复findviewbyid),这样优化后的getView方法写法如下:
View v = null;
if (convertView == null) {
v = LayoutInflater.from(context).inflate(R.layout.adapter__item, parent, false);
Holder vh = new Holder();
vh.text = (TextView) v.findViewById(R.id.text1);
v.setTag(vh);
} else {
v = convertView;
}
Holder vh = (Holder) v.getTag();
vh.text.setText(data[position]);
return v;
class ViewHolder{
ImageView img;
TextView price;
}
网友评论