主界面的ListView在android里面必须添加一个适配器MyBaseAdapter.java,Adapter是数据与Ui之间的桥梁,他把后台数据与前端客户端界面连接起来,他是展示数据的载体,其功能就是将每一个Item里面的属性显示出来,代码如下:
package com.example.jishiben;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.widget.TextView;
public class MyBaseAdapter extends BaseAdapter {
private List<Note> notes;
Context context;
public MyBaseAdapter(Context context, List<Note> note) {
this.notes = note;
this.context = context;
}
@Override
public int getCount() {
return (notes == null) ? 0 : notes.size();
}
@Override
public Object getItem(int position) {
return notes.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Note note = (Note) getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.list_view01_item, null);
}
TextView tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
TextView tvTime = (TextView) convertView.findViewById(R.id.tv_time);
ImageView tvimageView1=(ImageView) convertView.findViewById(R.id.imageView1);
if(note.getType() != null && note.getType().equals("备忘")) {
tvimageView1.setImageResource(R.drawable.ic_memo);
} else if(note.getType() != null && note.getType().equals("会议")) {
tvimageView1.setImageResource(R.drawable.ic_meeting);
} else if(note.getType() != null && note.getType().equals("纪念")) {
tvimageView1.setImageResource(R.drawable.ic_anni);
} else {
tvimageView1.setImageResource(R.drawable.ic_waitdo);
}
tvTitle.setText(note.getIntroduction());
tvTime.setText(note.getDate());
return convertView;
}
}
这里就不得不将ListView中每一个Item的布局展示一下,采用了一个ImageView用来显示事件类型图标,第一个TextView用来显示事件的内容,第二个TextView用来显示时间,list_view01_item.xml的布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ADFF2F">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_alignParentRight="true"
/>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_toLeftOf="@id/imageView1"
/>
<TextView
android:id="@+id/tv_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_title"
android:textSize="15sp"
android:layout_alignLeft="@id/tv_title" />
</RelativeLayout>
预知后事如何,请听下回分解.