标签:
public class MainActivity extends Activity { private ListView lv; private ArrayAdapter<String> arrayAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.listView1); String[] arr = { "中国1", "中国2", "中国3" }; arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr); lv.setAdapter(arrayAdapter); } }
// 1 新建一个新的数据适配器
// 2、适配器加载数据源
// 3、视图(ListView)加载适配器
ArrayAdapter(上下文,当前ListView加载的每一个列表项所对应的布局文件,数据源)
android.R.layout.simple_list_item_1与android.R.layout.simple_list_item_2与two_line_list_item的区别:
simple_list_item_1(单行显示)
此布局显示最为简单,其中只有一个TextView,id为:android.R.id.text1,显示的内容为
2、simple_list_item_2 和two_line_list_item(双行显示)
都有两个TextView:android.R.id.text1和android.R.id.text2,不同之处在于,前者两行字是不一样大小的,而后者中两行字体一样大小。
simple_list_item_2显示如图:
two_line_list_item
布局文件参考
http://zhidao.baidu.com/link?url=0vKixhhUdftduxjezRfBD0nC8h2ILG5SAyhDgNlT1xt9uaQSyR0l1lYoL6MCQd48OyXDsJ0wxhOVOqYh0SBq0_
simpleAdapter
private ListView lv; private SimpleAdapter simpleAdapter; private List<Map<String, Object>> datalist; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.listView1); datalist = new ArrayList<Map<String, Object>>(); // String a[] = new String[3]{"aa","bb","cc"};new // String[3]{"aa","bb","cc"}就是直接创建一个数组对象了,只是没有用个对象去接收 // ListView包含多少个列表项由数据源决定 /* * context:上下文 data: *数据源 (List<? extends Map<String, ?>>data)一个Map所组成的List集合 *每一个Map都会去对应ListView列表中的一行 * 每一个Map(键-值对)中的键必须包含所有在from中所指定的键 resource:列表项的布局文件ID from:Map中的键名 * to:绑定数据视图中的ID,与from成对应关系 */ simpleAdapter = new SimpleAdapter(this, getData(), R.layout.item, new String[] { "pic", "textView1" }, new int[] { R.id.pic, R.id.textView1 }); lv.setAdapter(simpleAdapter); } private List<Map<String, Object>> getData() { for (int i = 0; i < 20; i++) { //HashMap一对对数据的集合 Map<String, Object>map=new HashMap<String, Object>(); //拆分了看 只不过是加了键名和键值 Map map=new HashMap(); map.put("pic", R.drawable.ic_tubiao); map.put("textView1", "呵呵"+i); datalist.add(map); } return datalist; }
public class MainActivity extends Activity implements OnItemClickListener, OnScrollListener {
lv.setOnItemClickListener(this); lv.setOnScrollListener(this);
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub String text = lv.getItemIdAtPosition(position) + ""; Toast.makeText(this, "position=" + position + " " + "text=" + text, Toast.LENGTH_SHORT).show(); // Toast.LENGTH_LONG(3.5秒)和Toast.LENGTH_SHORT(2秒) // CharSequence类型 这是一个接口,代表的是一个有序字符集合 } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO Auto-generated method stub switch (scrollState) { case SCROLL_STATE_FLING: Log.i("Main", "用户在手指离开屏幕之前,由于用力滑了一下,视图仍然依靠惯性滑动"); Map<String, Object> map = new HashMap<String, Object>(); map.put("pic", R.drawable.ic_launcher); map.put("text", "增加项"); dataList.add(map); simpleAdapter.notifyDataSetChanged(); break; case SCROLL_STATE_IDLE: Log.i("Main", "视图已经停止滑动"); break; case SCROLL_STATE_TOUCH_SCROLL: Log.i("Main", "手指没有离开屏幕,视图正在滑动"); break; }}
标签:
原文地址:http://www.cnblogs.com/yi-mi-yangguang/p/5764500.html