Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.kman.AndroMail;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.kman.AndroMail.data.MailConstants;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.BaseColumns;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.View.OnClickListener;
- import android.widget.AdapterView;
- import android.widget.CursorAdapter;
- import android.widget.ListAdapter;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.AdapterView.OnItemClickListener;
- public class MessageListActivity extends Activity {
- private static final String TAG = "MessageListActivity";
- public static final String MESSAGE_LIST_URI_EXTRA = "MESSAGE_LIST_URI";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.messagelist);
- Intent intent = getIntent();
- mMessageListUri = intent.getParcelableExtra(MESSAGE_LIST_URI_EXTRA);
- mMessageListView = (ListView) findViewById(R.id.message_list);
- /*
- * Set list view click handler
- */
- mMessageListView.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parnet, View view, int position, long id) {
- onMessageClick(position, id);
- }
- });
- /*
- * Set list view adapter
- */
- mListCursor = managedQuery(mMessageListUri, MessageAdapter.gProjectionMap, null, null, null);
- mAdapter = new MessageAdapter(this, mListCursor);
- mMessageListView.setAdapter(mAdapter);
- int count = mListCursor.getCount();
- Log.i(TAG, "Cursor message count = " + String.valueOf(count));
- }
- private void onMessageClick(int position, long id) {
- Log.i(TAG, "onMessageClick: pos = " + String.valueOf(position) + ", id = " + id);
- Uri messageUri = Uri.withAppendedPath(mMessageListUri, String.valueOf(id));
- Intent intent = new Intent(this, MessageDisplayActivity.class);
- intent.putExtra(MessageDisplayActivity.MESSAGE_URI_EXTRA, messageUri);
- startActivity(intent);
- }
- private static final class MessageAdapter extends CursorAdapter {
- public static final String gProjectionMap[] = new String[] { BaseColumns._ID,
- MailConstants.MESSAGE_SUBJECT, MailConstants.MESSAGE_FROM,
- MailConstants.MESSAGE_WHEN, MailConstants.MESSAGE_MSG_ID };
- private static final int VIEW_TYPE_GROUP_START = 0;
- private static final int VIEW_TYPE_GROUP_CONT = 1;
- private static final int VIEW_TYPE_COUNT = 2;
- MessageAdapter(Context context, Cursor cursor) {
- super(context, cursor);
- mInflater = LayoutInflater.from(context);
- mColSubject = cursor.getColumnIndex(MailConstants.MESSAGE_SUBJECT);
- mColFrom = cursor.getColumnIndex(MailConstants.MESSAGE_FROM);
- mColWhen = cursor.getColumnIndex(MailConstants.MESSAGE_WHEN);
- mColMsgID = cursor.getColumnIndex(MailConstants.MESSAGE_MSG_ID);
- }
- @Override
- public int getViewTypeCount() {
- return VIEW_TYPE_COUNT;
- }
- @Override
- public int getItemViewType(int position) {
- /*
- * There is always a group header before the first data item
- */
- if (position == 0) {
- return VIEW_TYPE_GROUP_START;
- }
- /*
- * Get date for current and previous data items
- */
- Cursor cursor = getCursor();
- int oldPosition = cursor.getPosition();
- cursor.moveToPosition(position);
- Date dThis = new Date(cursor.getLong(mColWhen));
- cursor.moveToPosition(position - 1);
- Date dPrev = new Date(cursor.getLong(mColWhen));
- /*
- * Restore cursor position
- */
- cursor.moveToPosition(oldPosition);
- /*
- * Do grouping check
- *
- * This is bogus (compares day of month values), and should be
- * replaced to use day of year
- */
- if (dThis.getDay() != dPrev.getDay()) {
- return VIEW_TYPE_GROUP_START;
- }
- return VIEW_TYPE_GROUP_CONT;
- }
- @Override
- public View newView(Context context, Cursor cursor, ViewGroup parent) {
- int nViewType;
- int position = cursor.getPosition();
- if (position == 0) {
- /*
- * Group header for position 0
- */
- nViewType = VIEW_TYPE_GROUP_START;
- } else {
- /*
- * Compare date fields for this and previous items
- */
- Date dThis = new Date(cursor.getLong(mColWhen));
- cursor.moveToPosition(position - 1);
- Date dPrev = new Date(cursor.getLong(mColWhen));
- /*
- * This check is bogus and should be replaced
- */
- if (dThis.getDay() != dPrev.getDay()) {
- nViewType = VIEW_TYPE_GROUP_START;
- } else {
- nViewType = VIEW_TYPE_GROUP_CONT;
- }
- /*
- * Restore cursor position
- */
- cursor.moveToPosition(position);
- }
- View v;
- if (nViewType == VIEW_TYPE_GROUP_START) {
- /*
- * Inflate a layout for group start
- */
- v = mInflater.inflate(R.layout.message_list_item_with_header, parent, false);
- /*
- * Prevent clicking behaviour on the list header
- */
- View vHeader = v.findViewById(R.id.message_item_when_header);
- vHeader.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- }
- });
- } else {
- /*
- * Inflate a layout for intra-group item
- */
- v = mInflater.inflate(R.layout.message_list_item, parent, false);
- }
- return v;
- }
- @Override
- public void bindView(View view, Context context, Cursor cursor) {
- TextView tv;
- tv = (TextView) view.findViewById(R.id.message_item_subject);
- tv.setText(cursor.getString(mColSubject));
- tv = (TextView) view.findViewById(R.id.message_item_from);
- tv.setText(cursor.getString(mColFrom));
- tv = (TextView) view.findViewById(R.id.message_item_when);
- Date d = new Date(cursor.getLong(mColWhen));
- tv.setText(gDateFormatData.format(d));
- tv = (TextView) view.findViewById(R.id.message_item_id);
- tv.setText(cursor.getString(mColMsgID));
- /*
- * If there is a group header, set its value to just the date
- * (without the time)
- */
- tv = (TextView) view.findViewById(R.id.message_item_when_header);
- if (tv != null) {
- tv.setText(gDateFormatGroup.format(d));
- }
- }
- LayoutInflater mInflater;
- private int mColSubject;
- private int mColFrom;
- private int mColWhen;
- private int mColMsgID;
- private static SimpleDateFormat gDateFormatData = new SimpleDateFormat(
- "E, dd MMM yyyy HH:mm:ss");
- private static SimpleDateFormat gDateFormatGroup = new SimpleDateFormat("E, dd MMM yyyy");
- }
- private Uri mMessageListUri;
- private ListView mMessageListView;
- private Cursor mListCursor;
- private ListAdapter mAdapter;
- }
Advertisement
Add Comment
Please, Sign In to add comment