ListView的简单测试(记事本界面)

记下代码,方便以后查看。还没简化代码,仅仅是有界面。一下用到的是SimpleAdapter

首先是xml文件 activity_main.xml

这是总体框架布局文件

View Code
 1 <?xml version="1.0" encoding="utf-8"?>  
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
 3     android:layout_width="fill_parent"  
 4     android:layout_height="fill_parent"  
 5     >  
 6     <ListView  
 7     android:id="@+id/list"  
 8     android:layout_width="fill_parent"  
 9     android:layout_height="wrap_content"  
10     android:layout_alignParentTop="true"
11     /> 
12 
13     <LinearLayout
14         android:layout_width="fill_parent"  
15         android:layout_height="wrap_content"  
16         android:orientation="horizontal"
17         android:layout_alignParentBottom="true"
18         >        
19         <Button 
20             android:id="@+id/one"
21             android:layout_height="wrap_content"
22             android:layout_width="0dip"
23             android:layout_weight="1"
24             android:text="主界面"
25             />
26         <Button
27             android:id="@+id/two"
28             android:layout_height="wrap_content"
29             android:layout_width="0dip"
30             android:layout_weight="1"
31             android:text="新增"
32             />
33         <Button
34             android:id="@+id/three"
35             android:layout_height="wrap_content"
36            android:layout_width="0dip"
37             android:layout_weight="1"
38             android:text="查询"
39             />
40         <Button
41             android:id="@+id/four"
42             android:layout_height="wrap_content"
43            android:layout_width="0dip"
44             android:layout_weight="1"
45             android:text="创建分类"
46             />    
47         </LinearLayout>                                     
48 </RelativeLayout>  

另外的xml是ListView布局

View Code
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:id="@+id/Allpower"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical" >
 7  
 8 <RelativeLayout
 9     android:layout_width="fill_parent"
10     android:layout_height="wrap_content"
11     >
12         <TextView 
13             android:id="@+id/lvw_custom_name"
14             android:layout_width="wrap_content"
15             android:layout_height="wrap_content"            
16             android:layout_marginLeft="10dip"            
17             android:layout_marginTop="5dip"
18             android:textSize="25dip"
19             />
20         <TextView 
21             android:id="@+id/lvw_custom_description"
22             android:layout_width="wrap_content"
23             android:layout_height="wrap_content"
24             android:layout_alignParentRight="true"
25             android:layout_marginRight="10dip"
26             android:textSize="25dip"
27             />
28 </RelativeLayout>
29 </LinearLayout>

MainActivity.java

View Code
 1 package com.example.change;
 2 
 3 import java.util.ArrayList;
 4 import java.util.HashMap;
 5 import java.util.List;
 6 import java.util.Map;
 7 
 8 import android.R.anim;
 9 import android.os.Bundle;
10 import android.app.Activity;
11 import android.app.Dialog;
12 import android.view.Menu;
13 import android.view.View;
14 import android.widget.ArrayAdapter;
15 import android.widget.Button;
16 import android.widget.ListView;
17 import android.widget.SimpleAdapter;
18 
19 public class MainActivity extends Activity {
20 
21     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.activity_main);    
25         /*ListView lvsimple = (ListView)findViewById(R.id.list);
26         String item[] = new String [] {"one", "two", "three"}; 
27         ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, item);
28         lvsimple.setAdapter(adapter);*/
29         ListView lvwCustom = (ListView)findViewById(R.id.list);    
30         SimpleAdapter adapter = new SimpleAdapter(this, getData(),R.layout.lvw_custom,new String[] {"lvw_custom_name","lvw_custom_description"}, 
31                 new int[] {R.id.lvw_custom_name, R.id.lvw_custom_description});
32         lvwCustom.setAdapter(adapter);
33     }
34     
35 
36     //数据
37     private List<Map<String, String>> getData() {
38         //map.put(参数名字,参数值)  
39         List<Map<String, String>> list = new ArrayList<Map<String, String>>();         
40         Map<String, String> map = new HashMap<String, String>();
41         map.put("lvw_custom_name", "所有笔记");  
42         map.put("lvw_custom_description", "10");  
43         list.add(map);  
44 //this.getString(R.string.AllNoteNow)
45         map = new HashMap<String, String>();  
46         map.put("lvw_custom_name", "默认笔记");  
47         map.put("lvw_custom_description", "5");  
48         list.add(map);    
49         
50         map = new HashMap<String, String>();  
51         map.put("lvw_custom_name", "其他笔记");  
52         map.put("lvw_custom_description", "5");  
53         list.add(map);         
54         return list;
55     }
56     
57     @Override
58     public boolean onCreateOptionsMenu(Menu menu) {
59         // Inflate the menu; this adds items to the action bar if it is present.
60         getMenuInflater().inflate(R.menu.main, menu);
61         return true;
62     }
63 
64 }

 源码下载

原文地址:https://www.cnblogs.com/newlooker/p/3005919.html