android默认短信程序
主界面主要是一个ListView,每个Item上有一个QuickContactBadge(这里提到过])。这个可以通过定制ListView使用的adapter来实现。
查了些ListView自己写adapter的资料,
发现问题很多。主要集中于item带有事件响应,或者状态保存,例如Button、CheckBox,之类的控件时,将很难协调。
事实上,虽然QuickContactBadge也带有事件响应,但是集成到ListView的item里,相对而言简单很多。
ListView的Item配置:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<android.widget.QuickContactBadge
android:id="@+id/avatar"
android:layout_centerVertical="true"
android:src="@drawable/icon"
style="?android:attr/quickContactBadgeStyleWindowSmall" />
<TextView android:id="@+id/subject"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:singleLine="true"
android:layout_toRightOf="@id/avatar"
android:ellipsize="end" />
</RelativeLayout>
Adapter:
package com.kl.test;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.provider.ContactsContract.QuickContact;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.QuickContactBadge;
import android.widget.TextView;
public class QuickContactAdapter extends BaseAdapter {
public static final int RES_TEXT_ID = 1;
public static final int RES_BADGE_ID = 0;
private LayoutInflater mInflater;
private List<? extends Map<String, ?>> mData;
private String[] mFrom;
private int[] mTo;
private int mResource;
public final class ViewHolder{
public QuickContactBadge badge;
public TextView text;
}
public QuickContactAdapter(Context context, List<? extends Map<String, ?>> list, int resource,
String[] from, int[] to) {
mInflater = LayoutInflater.from(context);
mFrom = from;
mData = list;
mTo = to;
mResource = resource;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(mResource, null);
holder.text = (TextView)convertView.findViewById(mTo[RES_TEXT_ID]);
holder.badge = (QuickContactBadge)convertView.findViewById(mTo[RES_BADGE_ID]);
convertView.setTag(holder);
}
else {
holder = (ViewHolder)convertView.getTag();
}
holder.text.setText((String)mData.get(position).get(mFrom[RES_TEXT_ID]));
holder.badge.setMode(QuickContact.MODE_SMALL);
holder.badge.assignContactFromPhone((String)mData.get(position).get(mFrom[RES_BADGE_ID]), true);
return convertView;
}
}
其实,这个adapter的主要目的,是为对应的QuickContactBadge关联Uri。当然,我对ListView还不甚精通,这里就不细说了。anyway, it worked.
package com.kl.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class QuickContactList extends Activity {
List<Map<String, String>> mData = new ArrayList<Map<String, String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fillListView();
}
private void fillListView() {
ListView view = (ListView) findViewById(R.id.list);
List<Map<String, String>> data = mData;
for( int i = 0; i < 10; ++i ) {
Map<String, String> d1 = new HashMap<String, String>();
d1.put("text", String.format("Item %d", i));
d1.put("phone", String.format( "12%d", i));
data.add(d1);
}
QuickContactAdapter adapter = new QuickContactAdapter(this, data, R.layout.list_item,
new String[] { "phone", "text" },
new int[] { R.id.avatar, R.id.subject } );
view.setAdapter(adapter);
//view.setAdapter(new MyAdapter(this));
view.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
setTitle("点击第"+arg2+"个项目");
}
});
}
}
上面是测试
例子,实际运行效果QuickContactBadge不影响ListView的点击。