CursorAdapter的一段代碼
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 RecentCallsListActivity extends Activity {
/** Called when the activity is first created. */
private LinearLayout mEditLinearLayout;
private Button mAllSelectButton;
private Button mCancelSelectedButton;
private Button mDeleteButton;
private ListView mListView;
private TextView mEmptyTextView;
private QueryHandler mQueryHandler;
private RecentCallsListAdapter mAdapter;
private final static String[] CALLS_PROJECTION = new String[]{Calls._ID,Calls.NUMBER,Calls.DATE,
Calls.CACHED_NAME,Calls.TYPE};
private final static int ID_COLUMN_INDEX = 0;
private final static int NUMBER_COLUMN_INDEX = 1;
private final static int DATE_COLUMN_INDEX = 2;
private final static int CACHED_NAME_COLUMN_INDEX = 3;
private final static int TYPE_COLUMN_INDEX = 4;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initView();
}
private void initView() {
mEditLinearLayout = (LinearLayout) findViewById(R.id.edit);
mAllSelectButton = (Button) findViewById(R.id.button_allSelect);
mCancelSelectedButton = (Button) findViewById(R.id.button_cancelSelected);
mDeleteButton = (Button) findViewById(R.id.button_delete);
mListView = (ListView) findViewById(R.id.listView);
mEmptyTextView = (TextView) findViewById(R.id.emptyTextView);
mEditLinearLayout.setVisibility(View.GONE);
mDeleteButton.setVisibility(View.GONE);
mQueryHandler = new QueryHandler(getContentResolver());
mAdapter = new RecentCallsListAdapter(this, null);
mListView.setEmptyView(mEmptyTextView);
mListView.setAdapter(mAdapter);
startQuery();
}
private void startQuery(){
mQueryHandler.startQuery(0, null, Calls.CONTENT_URI, CALLS_PROJECTION, null, null, Calls.DEFAULT_SORT_ORDER);
}
final class RecentCallsListViews{
CheckBox mChechBox ;
ImageView mHeaderImageView;
TextView mNameTextView;
TextView mNumberTextView;
TextView mDateTextView;
TableLayout mTableLayout;
ImageView mCallTypeImage;
}
class RecentCallsListAdapter extends CursorAdapter{
public RecentCallsListAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(RecentCallsListActivity.this);
View view = inflater.inflate(R.layout.call_list_item, parent);
RecentCallsListViews views = new RecentCallsListViews();
views.mChechBox = (CheckBox) view.findViewById(R.id.mCheckbox);
views.mHeaderImageView = (ImageView) view.findViewById(R.id.mHeaderImageView);
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);
view.setTag(views);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
if(null == cursor || cursor.getCount() < 1){
return;
}
}
}
final class QueryHandler extends AsyncQueryHandler{
public QueryHandler(ContentResolver cr) {
super(cr);
// TODO Auto-generated constructor stub
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
// TODO Auto-generated method stub
super.onQueryComplete(token, cookie, cursor);
mAdapter.changeCursor(cursor);
}
}
}
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 RecentCallsListActivity extends Activity {
/** Called when the activity is first created. */
private LinearLayout mEditLinearLayout;
private Button mAllSelectButton;
private Button mCancelSelectedButton;
private Button mDeleteButton;
private ListView mListView;
private TextView mEmptyTextView;
private QueryHandler mQueryHandler;
private RecentCallsListAdapter mAdapter;
private final static String[] CALLS_PROJECTION = new String[]{Calls._ID,Calls.NUMBER,Calls.DATE,
Calls.CACHED_NAME,Calls.TYPE};
private final static int ID_COLUMN_INDEX = 0;
private final static int NUMBER_COLUMN_INDEX = 1;
private final static int DATE_COLUMN_INDEX = 2;
private final static int CACHED_NAME_COLUMN_INDEX = 3;
private final static int TYPE_COLUMN_INDEX = 4;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initView();
}
private void initView() {
mEditLinearLayout = (LinearLayout) findViewById(R.id.edit);
mAllSelectButton = (Button) findViewById(R.id.button_allSelect);
mCancelSelectedButton = (Button) findViewById(R.id.button_cancelSelected);
mDeleteButton = (Button) findViewById(R.id.button_delete);
mListView = (ListView) findViewById(R.id.listView);
mEmptyTextView = (TextView) findViewById(R.id.emptyTextView);
mEditLinearLayout.setVisibility(View.GONE);
mDeleteButton.setVisibility(View.GONE);
mQueryHandler = new QueryHandler(getContentResolver());
mAdapter = new RecentCallsListAdapter(this, null);
mListView.setEmptyView(mEmptyTextView);
mListView.setAdapter(mAdapter);
startQuery();
}
private void startQuery(){
mQueryHandler.startQuery(0, null, Calls.CONTENT_URI, CALLS_PROJECTION, null, null, Calls.DEFAULT_SORT_ORDER);
}
final class RecentCallsListViews{
CheckBox mChechBox ;
ImageView mHeaderImageView;
TextView mNameTextView;
TextView mNumberTextView;
TextView mDateTextView;
TableLayout mTableLayout;
ImageView mCallTypeImage;
}
class RecentCallsListAdapter extends CursorAdapter{
public RecentCallsListAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(RecentCallsListActivity.this);
View view = inflater.inflate(R.layout.call_list_item, parent);
RecentCallsListViews views = new RecentCallsListViews();
views.mChechBox = (CheckBox) view.findViewById(R.id.mCheckbox);
views.mHeaderImageView = (ImageView) view.findViewById(R.id.mHeaderImageView);
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);
view.setTag(views);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
if(null == cursor || cursor.getCount() < 1){
return;
}
}
}
final class QueryHandler extends AsyncQueryHandler{
public QueryHandler(ContentResolver cr) {
super(cr);
// TODO Auto-generated constructor stub
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
// TODO Auto-generated method stub
super.onQueryComplete(token, cookie, cursor);
mAdapter.changeCursor(cursor);
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.