读取通话记录,实现ListView的优化,并且是源码级的开发.很给力~
package cn.android.calls;import android.app.Activity;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog.Calls;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
private LinearLayout mEditLinearLayout;
private Button mAllSelectButton;
private Button mCancelSelectedButton;
private ListView mListView;
private TextView mEmptyTextView;
private Button mDeleteButton;
private QueryHandler mQueryHandler;
private RecentCallsListAdapter mAdapter;
private final static String[] PEOJECTION = new String[]{
Calls._ID,
Calls.CACHED_NAME,
Calls.NUMBER,
Calls.DATE,
Calls.TYPE
};
private final static int ID_COLUMN_INDEX = 0;
private final static int CACHED_NAME_COLUMN_INDEX = 1;
private final static int NUMBER_COLUMN_INDEX = 2;
private final static int DATE_COLUMN_INDEX = 3;
private final static int TYPE_COLUMN_INDEX = 4;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mQueryHandler = new QueryHandler(getContentResolver());
mAdapter = new RecentCallsListAdapter(this, null);
startQuery();
initView();
}
//初始化控件
private void initView() {
mEditLinearLayout = (LinearLayout) findViewById(R.id.edit);
mAllSelectButton = (Button) findViewById(R.id.allSelect);
mCancelSelectedButton = (Button) findViewById(R.id.cancelSelected);
mListView = (ListView) findViewById(R.id.listView);
mEmptyTextView = (TextView) findViewById(R.id.empty);
mDeleteButton = (Button) findViewById(R.id.deleteButton);
//如果没有通话记录就设置listView为空
mListView.setEmptyView(mEmptyTextView);
//将适配器绑定到listView上
mListView.setAdapter(mAdapter);
}
private void startQuery(){
mQueryHandler.startQuery(0, null, Calls.CONTENT_URI, PEOJECTION, null, null, Calls.DEFAULT_SORT_ORDER);
}
//将ListView查询到的组件都封装到这个类实例化的对象中,优化ListView
//如果ListView 过多 导致查询慢 所以需要优化
class RecentCallsListViews{
CheckBox mCheckBox;
ImageView mHeaderImageView;
TextView mNameTextView;
TextView mNumberTextView;
TextView mDateTextView;
TableLayout mTableLayout;
ImageView mCallTypeImageView;
}
final class RecentCallsListAdapter extends CursorAdapter{
public RecentCallsListAdapter(Context context,Cursor c){
super(context,c);
}
//抽象方法,在getView调用,用于产生数据
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.recent_call_list_item, parent, false);
RecentCallsListViews views = new RecentCallsListViews();
views.mCheckBox = (CheckBox) view.findViewById(R.id.check);
views.mHeaderImageView = (ImageView) view.findViewById(R.id.header);
views.mNameTextView = (TextView) view.findViewById(R.id.name);
views.mNumberTextView = (TextView) view.findViewById(R.id.number);
views.mDateTextView = (TextView) view.findViewById(R.id.date);
views.mTableLayout = (TableLayout) view.findViewById(R.id.call);
views.mCallTypeImageView = (ImageView) view.findViewById(R.id.type);
view.setTag(views);
return view;
}
//绑定数据
public void bindView(View view, Context context, Cursor cursor) {
if(cursor == null || cursor.getCount() < 1){
return;
}
RecentCallsListViews views = (RecentCallsListViews) view.getTag();
String idStr = cursor.getString(ID_COLUMN_INDEX);
String name = cursor.getString(CACHED_NAME_COLUMN_INDEX);
long date = cursor.getLong(DATE_COLUMN_INDEX);
String number = cursor.getString(NUMBER_COLUMN_INDEX);
int type = cursor.getInt(TYPE_COLUMN_INDEX);
if(name != null){
views.mNameTextView.setText(name);
}else if(number != null){
views.mNumberTextView.setText(number);
views.mDateTextView.setText(date+"");
}
}
}
//异步查询
final class QueryHandler extends AsyncQueryHandler{
public QueryHandler(ContentResolver cr) {
super(cr);
}
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
super.onQueryComplete(token, cookie, cursor);
mAdapter.changeCursor(cursor);
}
}
} --------------------编程问答-------------------- 学习啦... --------------------编程问答-------------------- 没看懂!偶是初学者。。。
补充:移动开发 , Android