1 <ListView
2 android:id="@+id/lv_info"
3 android:layout_width="match_parent"
4 android:layout_height5 android:dividerHeight="2dp"
6 android:divider="@color/colorDivider"
7 android:choiceMode="singleChoice"
8 android:listSelector="@color/colorSelected"
9 />
1 mListView= (ListView) this.findViewById(R.id.lv_info);
2 //1.初始化数据源
3 lstInfo=new ArrayList<>();
4 for(int i=0;i<20;i++){
5 lstInfo.add("Item=>"+i);
6 }
7 2.构造适配器
8 adapter=new ArrayAdapter<String>(MainActivity.,android.R.layout.simple_list_item_1,lstInfo);
9 3.给ListView赋值
10 addHeaderView必须在setAdapter之前设置,否则不起作用
11 TextView textView=new TextView(MainActivity.);
12 textView.setText("ListView的小例子"13 textView.setTextSize(2014 textView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
15 mListView.addHeaderView(textView);
16 mListView.setAdapter(adapter);
17 Button btnMore=new Button(MainActivity.18 btnMore.setText("加载更多..."19 textView.setTextSize(2020 btnMore.setLayoutParams(21 btnMore.setOnClickListener( View.OnClickListener() {
22 @Override
23 public void onClick(View v) {
24 int index=lstInfo.size();
25 int i=index;i<index+20;i++26 lstInfo.add("Item=>"+(i));
27 }
28 adapter.notifyDataSetChanged();
29 }
30 });
31 mListView.addFooterView(btnMore);
1 mListView.setOnItemClickListener( AdapterView.OnItemClickListener() {
2
3 /**
4 *
5 * @param parent 表示控件的主体,此处表示ListView
view 表示当前被点击的项对应的控件
7 position 表示当前点击项的索引位置
8 id 被点击的行的id
9 */
10 11 void onItemClick(AdapterView<?> parent,View view,int position,1)">long id) {
12 String s1 = parent.getItemAtPosition(position).toString();
13 Toast.makeText(MainActivity.this,"当前点击的是=>" + s1,Toast.LENGTH_SHORT).show();
14 16 mListView.setOnItemLongClickListener( AdapterView.OnItemLongClickListener() {
17
18 19 20 21 23 @return 如果返回true 表示被处理,只响应长按事件,如果返回false,表示还会响应单击事件
24 25 26 boolean onItemLongClick(AdapterView<?> parent,1)">27 String s1=parent.getItemAtPosition(position).toString();
28 Toast.makeText(MainActivity.s1,1)">29 return true;
31 });
1 <?xml version="1.0" encoding="utf-8"?>
2 LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width 4 android:layout_height="match_parent">
5 TextView
6 ="@+id/tv_name"
7 ="wrap_content"
8 android:layout_weight="1"
9 ="wrap_content"/>
11 ="@+id/tv_age"
12 13 14 15 16 ="@+id/tv_love"
17 18 19 </LinearLayout>
1 mListView2= (ListView) .findViewById(R.id.lv_info2);
1. 构造数据源
3 List<Map<String,Object>> lst=new ArrayList<Map<String,Object>> 5 Map<String,Object> map=new HashMap<String,Object> 6 map.put("colName","张三"+ 7 map.put("colAge",18+ 8 map.put("colLove","篮球"+ 9 lst.add(map);
11 2. 构造适配器填充
12 SimpleAdapter adapter=new SimpleAdapter(Main2Activity.13 R.layout.listview_item,1)">14 new String[]{"colName","colAge","colLove"},1)">15 new int[]{ R.id.tv_name,R.id.tv_age,R.id.tv_love});
16 3. 绑定适配器
17 mListView2.setAdapter(adapter);
1 mListView2.setOnItemClickListener( 4 String s1 = parent.getItemAtPosition(position).toString();此处返回的是键值对转换后的字符串
5 TextView tvName= (TextView) view.findViewById(R.id.tv_name);
6 TextView tvAge= (TextView) view.findViewById(R.id.tv_age);
7 TextView tvLove= (TextView) view.findViewById(R.id.tv_love);
8 String s2="Name=>"+tvName.getText()+"Age=>"+tvAge.getText()+"Love=>"+tvLove.getText();
9 Toast.makeText(Main2Activity.s2,1)">11 });