准备好ListView的数据之后,创建ListView需要三个步骤:
1)创建适配器
2)添加数据源到适配器
3)视图(即ListView)加载适配器,即setAdapter。
一、使用ArrayAdapter对象时,需要3个参数:
ArrayAdapter<类型> mArrayAdapter = new ArrayAdapter <类型>(context, textViewResourceID, data);
context上下文,即当前Activity,用this.
textViewResourceID:每个列表项对应的布局文件
data:数据源
注意:ArrayAdapter的功能有限,每个列表项只能是TextView。
二、创建SimpleAdapter对象时,需要5个参数
SimpleAdapter mSimpleAdapter = new SimpleAdapter(context, data, resource, from ,to);
其中:
context 上下文,当前Activity,即this.
data 为数据源,是List>类型的对象集合,每个Map对应一行列表项, 每个Map(键-值对)中的键必须包含所有在from中所指定的键
resource 列表项的布局文件ID
from:Map中的键名
to : 绑定数据视图中的ID,与from成对应关系。
三、单击、选中某个列表项,可通过AdapterView的setOnItemClickListener()方法、setOnSelectedListener()方法添加监听器。监测滚动变化,可用于视图在滚动中加载数据setOnSrcollListener()方法。
示例代码如下:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class SelectActivity extends Activity{
ListView listView1;
List> list;
SimpleAdapter listAdapter;
private String[] names = new String[]
{"小王","小李","小红","小张","小李","小红","小张","小李","小红","小张","小李","小红","小张"};
private String[] birthday = new String[]
{"2015/12/04","2014/10/14","2013/02/15","2012/06/22","2014/10/14",
"2013/02/15","2012/06/22","2014/10/14","2013/02/15","2012/06/22"
,"2014/10/14","2013/02/15","2012/06/22"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
listView1 = (ListView)findViewById(R.id.listView1);
list = new ArrayList>();
for(int i = 0; i < names.length; i++)
{
//给Hash表填入键值对
HashMap map = new HashMap();
map.put("username", names[i]);
map.put("user_birthday", birthday[i]);
list.add(map); //将每一个Map添加到ListView中
}
listAdapter = new SimpleAdapter(this,list,R.layout.item,
new String[]{"username","user_birthday"}, new int[]{R.id.user_name,R.id.user_birthday});
listView1.setAdapter(listAdapter);
listView1.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView>parent, View v, int position, long id){
Intent intent = getIntent();
intent.putExtra("username", names[position]+" "+birthday[position]);
SelectActivity.this.setResult(0,intent);
finish();
Toast.makeText(getApplicationContext(), "您单击了第"+position+"个", Toast.LENGTH_SHORT).show();
}
});
listView1.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView arg0, int scrollState) {
if(scrollState == SCROLL_STATE_FLING)
{
//给Hash表填入键值对
HashMap map = new HashMap();
map.put("username", "无名氏");
map.put("user_birthday", "2016-01-01");
list.add(map); //将每一个Map添加到ListView中
listAdapter.notifyDataSetChanged();//通知ListView更新列表
}
}
@Override
public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
});
}
}
运行结果:
在实现LIstView中发现,如果列表项不够多,不满屏,下拉刷新添加新列表项是不起作用的。