最近在制作一个小软件,需要用到控件ListView,就学习了如何使用。现在总结并跟大家交流一下

通过建立一个TimeTracker软件(用于记录自己跑步用的时间和一些notes)

使用ListView需要建立三个类。

1.TimeTracker.java;

2.TimeRecord.java;

3.TimeTrackerAdapter.java;

下面一一讲解这几个类如何使用:

一。 TimeTracker.java

      作用:用来显示软件主界面的。

      类型:activity--android

      xml:有。就是main.xml

      代码兼讲解:

 
  
  1. public class TimeTracker extends Activity { 
  2.     TimeTrackerAdapter timeTrackerAdapter; 
  3.     public void onCreate(Bundle savedInstanceState) 
  4.     { 
  5.         super.onCreate(savedInstanceState); 
  6.         setContentView(R.layout.main); 
  7.          
  8.         ListView listView = (ListView)findViewById(R.id.times_list); 
  9.         timeTrackerAdapter = new TimeTrackerAdapter(); 
  10.         listView.setAdapter(timeTrackerAdapter); 
  11.     } 

        xml代码:注意在这里main.xml的布局不再是LinearLayout了

 
  
  1. <ListView  
  2.     xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent"  
  5.     android:id="@+id/times_list" 
  6. /> 

 

二。TimeRecord.java

      作用:这个类是定义时间数据类。规定了时间存储格式

      类型:java.lang.Object

      xml:无

      代码兼讲解:

 
  
  1. public class TimeRecord 
  2.     private String time; 
  3.     private String notes; 
  4.      
  5.     public TimeRecord(String time, String notes) 
  6.     { 
  7.         this.time = time; 
  8.         this.notes = notes; 
  9.     } 
  10.     public String getTime() { return time; } 
  11.     public void setTime(String time) { this.time = time; } 
  12.     public String getNotes() { return notes; } 
  13.     public void setNotes(String notes) { this.notes = notes; } 

       set()是给属性赋值的,get()是取得属性值的,被设置和存取的属性一般是私有,主要是起到封装的作用,不允许直接对属性操作。
        set()和get()不一定同时存在,看程序需求。这表现了面向对象的一种特性。

三。TimeTrackerAdapter.java

      作用:我们最最重要主角来了!它相当于一个中介,把抽象的TimeRecord放到实际的ListView上!

      类型:Adapter

     xml:有,time_list_item.xml

      代码兼讲解:

 
  
  1. public class TimeTrackerAdapter extends BaseAdapter { 
  2.  
  3.        private ArrayList<TimeRecord> times = new ArrayList<TimeRecord>();//建立一个arrayList<>里面应该表示的是转换object的内容,  
  4.         //然后这个数组叫做Time 
  5.     public TimeTrackerAdapter() 
  6.     { 
  7.         times.add(new TimeRecord("17:48", "tired")); 
  8.         times.add(new TimeRecord("15:39", "hurray!")); 
  9.     } 
  10.   
  11.     public int getCount() 
  12.     { 
  13.         return times.size(); 
  14.     } 
  15.     public Object getItem(int index) 
  16.     { 
  17.         return getItem(index); 
  18.     } 
  19.     public long getItemId(int index) 
  20.     { 
  21.         return index; 
  22.     } 
  23.     public View getView(int index, View view, ViewGroup parent) 
  24.     { 
  25.         if(view == null) 
  26.         {       LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 
  27.         view = inflater.inflate(R.layout.time_list_item, parent, false); 
  28.         } 
  29.         TimeRecord time = times.get(index); 
  30.         TextView timeTextView = (TextView)view.findViewById(R.id.time_view); 
  31.         timeTextView.setText(time.getTime()); 
  32.         TextView notesTextView = (TextView)view.findViewById(R.id.notes_view); 
  33.         notesTextView.setText(time.getNotes()); 
  34.         return view;     
  35.     } 

      xml代码:

 
  
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <LinearLayout 
  3.     xmlns:android="http://schemas.android.com/apk/res/android" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="wrap_content" 
  6.     android:orientation="vertical"
  7.     <TextView  
  8.         android:id="@+id/time_view" 
  9.         android:layout_width="fill_parent" 
  10.         android:layout_height="wrap_content" 
  11.         android:textSize="18dp" 
  12.         android:paddingBottom="5dp" /> 
  13.     <TextView 
  14.         android:id="@+id/notes_view" 
  15.         android:layout_width="fill_parent" 
  16.         android:layout_height="wrap_content" 
  17.         android:textSize="12dp" /> 
  18. </LinearLayout> 

效果图!

 

祝大家成功!!

第一次写如此技术的博客。。有不足的地方欢迎大家指教!!

谢谢!!