最近在制作一个小软件,需要用到控件ListView,就学习了如何使用。现在总结并跟大家交流一下
通过建立一个TimeTracker软件(用于记录自己跑步用的时间和一些notes)
使用ListView需要建立三个类。
1.TimeTracker.java;
2.TimeRecord.java;
3.TimeTrackerAdapter.java;
下面一一讲解这几个类如何使用:
一。 TimeTracker.java
作用:用来显示软件主界面的。
类型:activity--android
xml:有。就是main.xml
代码兼讲解:
- public class TimeTracker extends Activity {
- TimeTrackerAdapter timeTrackerAdapter;
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ListView listView = (ListView)findViewById(R.id.times_list);
- timeTrackerAdapter = new TimeTrackerAdapter();
- listView.setAdapter(timeTrackerAdapter);
- }
xml代码:注意在这里main.xml的布局不再是LinearLayout了
- <ListView
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:id="@+id/times_list"
- />
二。TimeRecord.java
作用:这个类是定义时间数据类。规定了时间存储格式
类型:java.lang.Object
xml:无
代码兼讲解:
- public class TimeRecord
- {
- private String time;
- private String notes;
- public TimeRecord(String time, String notes)
- {
- this.time = time;
- this.notes = notes;
- }
- public String getTime() { return time; }
- public void setTime(String time) { this.time = time; }
- public String getNotes() { return notes; }
- public void setNotes(String notes) { this.notes = notes; }
- }
set()是给属性赋值的,get()是取得属性值的,被设置和存取的属性一般是私有,主要是起到封装的作用,不允许直接对属性操作。
set()和get()不一定同时存在,看程序需求。这表现了面向对象的一种特性。
三。TimeTrackerAdapter.java
作用:我们最最重要主角来了!它相当于一个中介,把抽象的TimeRecord放到实际的ListView上!
类型:Adapter
xml:有,time_list_item.xml
代码兼讲解:
- public class TimeTrackerAdapter extends BaseAdapter {
- private ArrayList<TimeRecord> times = new ArrayList<TimeRecord>();//建立一个arrayList<>里面应该表示的是转换object的内容,
- //然后这个数组叫做Time
- public TimeTrackerAdapter()
- {
- times.add(new TimeRecord("17:48", "tired"));
- times.add(new TimeRecord("15:39", "hurray!"));
- }
- public int getCount()
- {
- return times.size();
- }
- public Object getItem(int index)
- {
- return getItem(index);
- }
- public long getItemId(int index)
- {
- return index;
- }
- public View getView(int index, View view, ViewGroup parent)
- {
- if(view == null)
- { LayoutInflater inflater = LayoutInflater.from(parent.getContext());
- view = inflater.inflate(R.layout.time_list_item, parent, false);
- }
- TimeRecord time = times.get(index);
- TextView timeTextView = (TextView)view.findViewById(R.id.time_view);
- timeTextView.setText(time.getTime());
- TextView notesTextView = (TextView)view.findViewById(R.id.notes_view);
- notesTextView.setText(time.getNotes());
- return view;
- }
xml代码:
- <?xml version="1.0" encoding="UTF-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <TextView
- android:id="@+id/time_view"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="18dp"
- android:paddingBottom="5dp" />
- <TextView
- android:id="@+id/notes_view"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="12dp" />
- </LinearLayout>
效果图!
祝大家成功!!
第一次写如此技术的博客。。有不足的地方欢迎大家指教!!
谢谢!!